【发布时间】:2017-02-28 18:58:54
【问题描述】:
如何在列表中获取完整的字典数据。但首先我需要检查它们是否存在键和值并配对。
test = [{'a': 'hello' , 'b': 'world', 'c': 1},
{'a': 'crawler', 'b': 'space', 'c': 5},
{'a': 'jhon' , 'b': 'doe' , 'c': 8}]
当我试图让它像这样有条件时
if any((d['c'] is 8) for d in test):
值是真或假,但我希望结果是像字典一样
{'a': 'jhon', 'b': 'doe', 'c': 8}
和我一样
if any((d['a'] is 'crawler') for d in test):
结果是:
{'a': 'crawler', 'b': 'space', 'c': 5}
提前致谢
【问题讨论】:
标签: python list if-statement dictionary conditional-statements