Python数组乘法 [英] Python array multiply
本文介绍了Python数组乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hh = [[82.5],[168.5]]
N = 1. / 5
ll = N * hh
我做错了什么?我收到错误:
$ b
无法乘以$ b $的非int类型的序列'float'
我尝试添加float(),但这不能解决我的问题;
我需要乘以数组中的每个元素...
感谢所有
为数组*,但如何乘数组数组,我试着相同的数组*,但有问题:
编辑2:**
hh = [[82.5],[168.5]]
N = zip(* hh)
ll = [[x * N for x in y] for y in hh]
???
解决方案
当你在Python中乘以 X
时,它不会乘以序列 - 它所做的是重复序列 X
次。这就是为什么X必须是一个整数(它不能是一个浮点数)。
你想要做的是使用列表理解:
hh = [[82.5],[168.5]]
N = 1.0 / 5
ll = [[x * N for x in y] for y in hh]
hh=[[82.5], [168.5]]
N=1./5
ll=N*hh
What I'm doing wrong? I received error :
"can't multiply sequence by non-int of type 'float'"
I try to add float(), but this is not solve my problem;
I need to multiply each element in array... thanks to all
**Ok thanks for idea for number * array, but how to multiply array*array, I tried same as number*array, but have problems:
EDIT 2:**
hh=[[82.5], [168.5]]
N=zip(*hh)
ll = [[x*N for x in y] for y in hh]
???
解决方案
When you multiply a sequence by X
in Python, it doesn't multiply each member of the sequence - what it does is to repeat the sequence X
times. That's why X has to be an integer (it can't be a float).
What you want to do is to use a list comprehension:
hh = [[82.5], [168.5]]
N = 1.0 / 5
ll = [[x*N for x in y] for y in hh]
这篇关于Python数组乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文