【发布时间】:2012-06-27 03:29:58
【问题描述】:
更新:为了清楚起见,我想检查 'name' 和 'last' 的键值,并仅在它们不在列表中时添加。
我有:
lst = [{'name':'John', 'last':'Smith'.... .... (other key-values)... },
{'name':'Will', 'last':'Smith'... ... (other key-values)... }]
只有当它与现有字典不完全相同时,我才想将一个新字典附加到此列表中。
换句话说:
dict1 = {'name':'John', 'last':'Smith'} # ==> wouldn't be appended
但是……
dict2 = {'name':'John', 'last':'Brown'} # ==> WOULD be appended
谁能解释一下最简单的方法,以及用英语解释解决方案中发生的事情。谢谢!
参考:Python: Check if any list element is a key in a dictionary
【问题讨论】:
标签: python list dictionary