Pythonプログラミング:NameError:name'reduce 'が定義されていません



Python Programming Nameerror



問題が来ています

reduce()テストを使用すると、エラーが発生します。 削減は定義されていません!

print(reduce(lambda x, y: x + y, [ 1, 2, 3])) '''Output: NameError: name 'reduce' is not defined '''

解決する

Quote answer from stackoverflow: - You are using python3 - Refer to the python2 guide from functools import reduce # py3 print(reduce(lambda x, y: x + y, [ 1, 2, 3])) '''Output: 6 '''

python3の組み込み関数からreduce関数が削除され、functoolsモジュールが追加されました。



参照:NameError:グローバル名「reduce」が定義されていません
接続: https://stackoverflow.com/questions/10226381/nameerror-global-name-reduce-is-not-defined
元の: https://blog.csdn.net/mouday/article/details/78910056