您当前的位置: 首页 >  Python

彭世瑜

暂无认证

  • 0浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python编程:NameError: name 'reduce' is not defined

彭世瑜 发布时间:2017-12-27 11:12:08 ,浏览量:0

问题来了

使用 reduce() 测试的时候报错:reduce 未定义!

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

"""Output:
NameError: name 'reduce' is not defined
"""
解决

引用stackoverflow的回答: - 你使用的是python3 - 参考的是python2的指南

from functools import reduce  # py3

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

"""Output:
6
"""

reduce函数在python3的内建函数移除了,放入了functools模块

参考: NameError: global name ‘reduce’ is not defined 连接: https://stackoverflow.com/questions/10226381/nameerror-global-name-reduce-is-not-defined

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.2582s