python - 如何乘以列表列表的位置

标签 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 做到这一点?

最佳答案

您可以在纯 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 ])

关于python - 如何乘以列表列表的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50468792/

相关文章:

python - Pyplot 堆积直方图 - 列中出现的次数

c - 未指定两个边界的二维数组

javascript - Jquery多维数组

python - python-daemon 中的信号处理

python - Python 中的枚举 : How to enforce in method arguments

python - 如何优雅地处理请求中的连接错误?

Python - 在 Slack API 中添加链接

python - Heroku 实例是否持久? (或者,我可以使用字典/数组作为缓存吗?)

arrays - 计算 x 和 y 之间的值在数组中出现的次数

python - Gunicorn worker 无论如何都会超时