Python相当于MATLAB命令prod

Sop*_*hie 2 python matlab porting numpy

是否有一个python等效的MATLAB命令"prod"(这里描述)?

Ash*_*ary 7

你可以reduce在Python中使用:

>>> from operator import mul
>>> reduce(mul, range(1, 5))
24
Run Code Online (Sandbox Code Playgroud)

或者,如果你已经numpy那么最好使用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])
Run Code Online (Sandbox Code Playgroud)