如何用 python 乘以这个列表:
A = [ [0.45, 0.89, 0.91],
[0.5, 0.78, 0.55],
[0.134, 0.571, 0.142] ]
如何将每一列相乘,例如 0.45*0.5*0.134 = 0.03015; 0.89*0.78*0.571 = 0.3961; 0,91*0.55*0.142 = 0.071071
[0.03015,0.3961,0.071071]
我怎样才能用 python 做到这一点?
请您参考如下方法:
您可以在纯 Python 中执行此操作:
from operator import mul
from functools import reduce # no need for this in Python 2.x
res = [reduce(mul, i) for i in zip(*A)]
或者你可以使用numpy:
import numpy as np
res = np.prod(A, axis=0)
array([ 0.03015 , 0.3963882, 0.071071 ])
