Home = { 'oranges': 0, 'apples': 2, 'bananas': 1 }
Work = { 'oranges': 1, 'apples': 0, 'bananas': 4 }
locations = [Home, Work]
store = [d for d in locations if 'oranges' in d and d.get('oranges') < 1]
>>> store
[{'apples': 2, 'oranges': 0, 'bananas': 1}]
这将使用列表理解来完成。它迭代字典列表
locations
,并测试每个字典的'orange'
键的值:注意不要包含没有
'orange'
作为键的字典注意,应该使用小写变量名,如
home
和work
相关问题 更多 >
编程相关推荐