【发布时间】:2017-11-09 05:58:16
【问题描述】:
我正在学习如何使用filter 函数。
这是我写的代码:
people = [{'name': 'Mary', 'height': 160},
{'name': 'Isla', 'height': 80},
{'name': 'Sam'}]
people2 = filter(lambda x: "height" in x, people)
如您所见,我正在尝试删除所有不包含 'height' 键的字典。
代码可以正常工作,事实上,如果我这样做:
print(list(people2))
我明白了:
[{'name': 'Mary', 'height': 160}, {'name': 'Isla', 'height': 80}]
问题是如果我做两次:
print(list(people2))
print(list(people2))
第二次,我得到一个空列表。
你能解释一下为什么吗?
【问题讨论】:
标签: python python-3.x