python3中的reduce()函数

在Python 3里,reduce()函数已经被从全局命名空间里移除了,它现在被放置在fucntools模块里。要使用reduce()的话,要先引入from functools import reduce   

from functools import reduce
def prod(x,y):
    return x * y

print ( reduce(prod, [2, 4, 5, 7, 12]) );

>>> 3360

猜你喜欢

转载自blog.csdn.net/qq_37811638/article/details/83057811