NameError: name 'reduce' is not defined

python3中的reduce函数和python2.7不在同一个库里了,使用的时候要注意。Python3使用reduce函数要先:from functools import reduce 比如:

from functools import reduce 

print(reduce(lambda x, y: x+y, [1,2,3,4,5]))

Python reduce() 函数的用法可以参考:http://www.runoob.com/python/python-func-reduce.html

猜你喜欢

转载自blog.csdn.net/qq_27871973/article/details/81128061