如何乘以列表列表的位置

时间:2018-05-22 13:20:12

标签: python arrays python-2.7 list numpy

如何将此列表与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做到这一点?

4 个答案:

答案 0 :(得分:12)

你可以用纯Python做到这一点:

numpy

或者您可以使用import numpy as np res = np.prod(A, axis=0) array([ 0.03015 , 0.3963882, 0.071071 ])

componentDidMount

答案 1 :(得分:3)

您可以使用 zip

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]

答案 2 :(得分:2)

使用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)

答案 3 :(得分:1)

使用zip并列出解包:

>>> [x*y*z for x, y, z in zip(*A)]