繁体   English   中英

嵌套列表的元素乘法

[英]Element wise multiplication of nested lists

我在长度为N的列表中嵌套了相同长度的列表:

[[13, 12, 12, 66], [13, 12, 66, 12], [13, 66, 12, 12], [78, 12, 12, 12], ...]

结果应该是列表中所有列表的元素乘积:

[171366, 114048, 114048, 114048, ...]

我能想到的一种可能的解决方案是使用带有np.multiply(list[index], list[index + 1])的 for 循环,但这会导致 go 的索引超出范围。

使用numpy.prod

l = [[13, 12, 12, 66], [13, 12, 66, 12], [13, 66, 12, 12], [78, 12, 12, 12]]
np.prod(l, axis=0)

Output:

array([171366, 114048, 114048, 114048])

math.prod 可以像这样使用(其中 L 是列表的列表):

from math import prod
M = [prod(x) for x in zip(*L)]

[171366, 114048, 114048, 114048]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM