我正在尝试将Lambda函数与filter()
一起使用,但收到错误消息:"TypeError: 'list' object is not callable"
。
我的代码如下:
list1 = [1,2,3,4,5,6,7,8,9]
newlist = list(filter(lambda a:(a%2==0), list1))
print(newlist)
预期输出:
[2,4,6,8]
错误:
TypeError Traceback (most recent call last) <ipython-input-48-139f323ff9a1> in <module>()
1 list1 = [1,2,3,4,5,6,7,8,9]
----> 2 newlist = list(filter(lambda a:(a%2==0), list1))
3 print(newlist)
TypeError: 'list' object is not callable
注意:-如果我们将命令用作"newlist = tuple(filter(lambda a:(a%2==0), list1))"
,我们将得到以下输出,并且没有错误:
(2, 4, 6, 8)
答案 0 :(得分:1)
您可能在其他地方将其定义为列表,请尝试重置您的IDE, 我将您的代码(您发布的3行代码)复制到了我的jupyter笔记本中,它运行良好 as seen in this image