如何在Python中对list
(向量)执行成员操作,比如在Matlab/Octave中。我是说,给出两个清单:
a = [1,2,3]
b = [4,5,6]
是否存在成员运算符,例如:
^{pr2}$我知道我可以自己实现它,如果没有这样的模块我也会这么做。不完全防弹的方法可以是:
# Memberwise product:
def mwprod(a,b):
c = []
if len(a) == len(b):
for a,b in zip(a,b):
try:
c.append(a*b)
except:
c.append(NaN)
return c
正如user3426575
所述,这可以用一种非常Python的方式浓缩:
c = [ x*y for x, y in zip(a,b) ]
不管怎样,我想找一个更密集、更美观的方法来重载或实现list
上的此类运算符。在
你可以这样尝试
这可以用numpy实现:
相关问题 更多 >
编程相关推荐