简体   繁体   English

如何从值不同的两个字典中查找公用密钥

[英]how to find common keys from two dict with difference in values

I am trying to get the keys whose values differ in both dictionaries eg: 我正在尝试获取两个字典中值不同的键,例如:

items1=['a','b','c']
price1=[1,2,3]
dictA= dict(zip(items1, price1))

items2=['a','b']
price2=[1,3]
dictB=dict(zip(items2,price2))

so the difference will be ['b'] as this key is the only difference 因此差异为['b'],因为此键是唯一的差异

i tried using set(dictA.items()).symmetric_difference(dictB.items()) , but this is also returning the key:value {'c':3} 我尝试使用set(dictA.items()).symmetric_difference(dictB.items()) ,但这也返回了key:value {'c':3}

Iterate over the common keys, and drop keys having matching values in dictA and dictB : 遍历公共键,并放下在dictAdictB具有匹配值的键:

In [3]: {key for key in dictA.keys() & dictB if dictA[key] != dictB[key]}
Out[3]: {'b'}

您必须在交叉路口上进行迭代。

delta = [k for k in (set(dictA) & set(dictB)) if (dictB[k] - dictA[k])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM