This question already has answers here:
Returning the product of a list
(14个答案)
5年前关闭。
是否有与MATLAB命令“prod”(已描述here)等效的python?

最佳答案

您可以在Python中使用reduce

>>> from operator import mul
>>> reduce(mul, range(1, 5))
24

或者如果你有核,那么最好使用numpy.prod
>>> import numpy as np
>>> a = np.arange(1, 10)
>>> a.prod()
362880
#Product along a axis
>>> a = np.arange(1, 10).reshape(3,3)
>>> a.prod(axis=1)
array([  6, 120, 504])

关于python - Python相当于MATLAB命令prod ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20158359/

10-12 18:50