Comparing Two Dictionaries and Checking How Many (Key, Value) Pairs Are Equal

Comparing two dictionaries and checking how many (key, value) pairs are equal

If you want to know how many values match in both the dictionaries, you should have said that :)

Maybe something like this:

shared_items = {k: x[k] for k in x if k in y and x[k] == y[k]}
print(len(shared_items))

Python3 Determine if two dictionaries are equal

== works

a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
a == b == c == d == e
True

I hope the above example helps you.

How to get the difference between two dictionaries in Python?

Try the following snippet, using a dictionary comprehension:

value = { k : second_dict[k] for k in set(second_dict) - set(first_dict) }

In the above code we find the difference of the keys and then rebuild a dict taking the corresponding values.

Python3: Comparing a specific pair of keys and values between two dictionaries

You could merge the dicts on equal (key, value) pairs and then run subsequent checks against the merged dict.

>>> dictA = {1:'Y', 2:'E', 3:'E'}
>>> dictB = {1:'Y', 2:'A', 3:'W'}
>>>
>>> merged = dict(dictA.items() & dictB.items())
>>> merged
{1: 'Y'}
>>>
>>> 1 in merged
True
>>> 3 in merged
False

Creating merged is painless because the return values of dict.keys, dict.values and dict.items support the set interface with regards to operations such as union, intersection, etc.

Caveat: requires hashable values in your dicts. If you have unhashable values, create merged via

>>> b_items = dictB.items()
>>> merged = dict(pair for pair in dictA.items() if pair in b_items)

Checking if two dictionaries of key-val pairs with values as array are equal

== with dictionaries compares keys and values. But the values are arrays. array1 == array2 produces a boolean array, which does not play well with the yes/no expectations of the dictionary test (the ambiguity error).

A way around that is to compare the values individually. np.allclose is the best test for float arrays. Assuming that the keys match, the following list comprehension works nicely:

In [177]: array=np.array                                                             
In [178]: a = {0: array([4.5, 5. ]), 1: array([3.5, 4.5]), 2: array([1., 1.])}
...: b = {0: array([4., 5. ]), 1: array([3, 4]), 2: array([1.5, 1.])}
In [179]: a
Out[179]: {0: array([4.5, 5. ]), 1: array([3.5, 4.5]), 2: array([1., 1.])}
In [180]: b
Out[180]: {0: array([4., 5.]), 1: array([3, 4]), 2: array([1.5, 1. ])}
In [181]: [np.allclose(a[k],b[k]) for k in a]
Out[181]: [False, False, False]
In [182]: [np.allclose(a[k],a[k]) for k in a]
Out[182]: [True, True, True]

There should be another lay of testing, for equal keys.

However allclose does not work if the arrays differ in shape:

In [183]: c = {0: array([4., 5., 0 ]), 1: array([3, 4]), 2: array([1, 1.])}          
In [185]: [np.allclose(a[k],c[k]) for k in a]
....
ValueError: operands could not be broadcast together with shapes (2,) (3,)

The comparison task will be a lot simpler if you know how the dictionaries might differ. Can they differ in keys? Can they differ in types of the values (array vs a list vs a number)? If values are arrays, can they differ in shape?

Comparing Two Dictionaries Key Values and Returning the Value If Match

You need to iterate over the keys in crucial and compare each one against the dishes keys. So a directly modified version of your code.

for key in crucial.keys():
if key in dishes.keys():
print(dishes[key])

It could be better stated as (no need to specify .keys):

for key in crucial:
if key in dishes:
print(dishes[key])

Compare the keys of two dictionaries and create a dictionary with key, value pairs that does not match the key in the other dictionary - Python

You can perform set-like operations on the result of dict.keys:

For set-like views, all of the operations defined for the abstract base class collections.abc.Set are available (for example, ==, <, or ^).

So you can do something like

dict_1 = {'a':'1', 'b':'1', 'c': None}
dict_2 = {'d': '1', 'a': '2'}

keys = dict_1.keys() ^ dict_2.keys()

new_dict = {k: dict_1.get(k, dict_2.get(k)) for k in keys}


Related Topics



Leave a reply



Submit