Python 的 sum()
函数返回一个可迭代对象的总和。
sum([3,4,5]) == 3 + 4 + 5 == 12
我正在寻找返回产品的函数。
somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60
我很确定这样的功能存在,但我找不到。
最佳答案
实际上,Guido 否决了这个想法:http://bugs.python.org/issue1093
但是,正如该问题所述,您可以很容易地制作一个:
from functools import reduce # Valid in Python 2.6+, required in Python 3
import operator
reduce(operator.mul, (3, 4, 5), 1)
https://stackoverflow.com/questions/595374/