如何将此列表与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做到这一点呢?
发布于 2018-05-22 21:25:17
您可以在纯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 ])发布于 2018-05-22 21:24:37
您可以使用,
In [5]: A = [[0.45, 0.89, 0.91], [0.5, 0.78, 0.55], [0.134, 0.571, 0.142]]
In [6]: [a*b*c for a,b,c in zip(*A)]
Out[6]: [0.030150000000000003, 0.39638819999999997, 0.071071]发布于 2018-05-22 21:23:10
使用numpy:
import numpy
A = numpy.array([[0.45, 0.89, 0.91], [0.5, 0.78, 0.55], [0.134, 0.571, 0.142]])
result = numpy.product(A, axis=0)https://stackoverflow.com/questions/50468792
复制相似问题