NameError: name 'reduce' is not defined 解決

python3.x中的reduce函數和python2.x不在同一個庫裏了,需要導入:from functools import reduce

from functools import reduce
print('the total is:',reduce((lambda x,y:x + y),range(5)))

運行結果:

 

the total is: 10

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章