Python NameError: name 'reduce' is not defined
2017-07-11 13:08
756 查看
今天在搜用Python求阶乘的时候, 搜出来的最简单的是用reduce这个built-in function, 但是我在用reduce的时候, 却报NameError: name 'reduce'
is not defined. 于是又搜了一下,发现在python 3.0.0.0以后, reduce已经不在built-in function里了, 要用它就得from functools import
reduce.
详见The fate of reduce() in Python 3000
reduce的用法
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
意思就是对sequence连续使用function, 如果不给出initial, 则第一次调用传递sequence的两个元素, 以后把前一次调用的结果和sequence的下一个元素传递给function. 如果给出initial, 则第一次传递initial和sequence的第一个元素给function.
[python]
view plain
copy
>>> from functools import reduce
>>> reduce(lambda x,y: x+y, [1, 2, 3])
6
>>> reduce(lambda x, y: x+y, [1,2,3], 9)
15
>>> reduce(lambda x,y: x+y, [1, 2, 3], 7)
13
>>>
is not defined. 于是又搜了一下,发现在python 3.0.0.0以后, reduce已经不在built-in function里了, 要用它就得from functools import
reduce.
详见The fate of reduce() in Python 3000
reduce的用法
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
意思就是对sequence连续使用function, 如果不给出initial, 则第一次调用传递sequence的两个元素, 以后把前一次调用的结果和sequence的下一个元素传递给function. 如果给出initial, 则第一次传递initial和sequence的第一个元素给function.
[python]
view plain
copy
>>> from functools import reduce
>>> reduce(lambda x,y: x+y, [1, 2, 3])
6
>>> reduce(lambda x, y: x+y, [1,2,3], 9)
15
>>> reduce(lambda x,y: x+y, [1, 2, 3], 7)
13
>>>
相关文章推荐
- Python编程:NameError: name 'reduce' is not defined
- Python 遇到NameError: name '_name_' is not defined这样的错误
- Python使用eval强制转换字符串为字典时报错:File "<string>", line 1, in <module> NameError: name 'nan' is not defined
- 【python】-- NameError: name 'reload' is not defined
- Python中,用eval强制将字符串转换为字典变量时候出错:NameError: name 'null' is not defined
- Python 报错 NameError: name '_name_' is not defined
- python3错误 之NameError: name 'cookielib' is not defined
- Python NameError: name ‘null’ is not defined
- Python编程中遇到NameError: name 'reload' is not defined 问题以及Python编码问题
- python2.7以下出 NameError: global name 'FileNotFoundError' is not defined的解决方案
- Python编译时提示“NameError: name 'Popen' is not defined”及”NameError: name 'PIPE' is not defined“错误的解决
- 【Python】NameError:name ‘xrange’ is not defined
- Python 解决 :NameError: name 'reload' is not defined 问题
- 关于python-NameError: global name 'platform' is not defined
- paip.python NameError name 'xxx' is not defined\
- python NameError: name 'raw_input' is not defined
- python之NameError: name 'raw_input' is not defined
- python提示NameError: name 'xxx' is not defined
- python 学习day2 NameError: name '_name_' is not defined
- Python问题之“NameError: name 'reload' is not defined”