这是因为 map 函数实现的性质,map、reduce 和 filter 的第二个参数是列表,但它如何读取并将值传递给这些方法的第一个参数不同,如果您在下面看到 reduce 函数默认从列表中选择 2 个元素并传递给 lambda 但是 filter 和 map 只需要一个元素。
>>>reduce(lambda x,y: x+ y,range(10))
45
>>>filter(lambda x,y: x+ y,range(10))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 2 arguments (1 given)
>>>filter(lambda x: x+ 1,range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>>map(lambda x,y: x+ y,range(10))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 2 arguments (1 given)
>>>
>>>map(lambda x: x+1, range(10))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>