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
i tried using set(dictA.items()).symmetric_difference(dictB.items())
, but this is also returning the key:value {'c':3}
Iterate over the common keys, and drop keys having matching values in dictA
and dictB
:
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])]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.