Comparison of 2 dictionaries: the same key, inappropriate values

I am currently trying to compare 2 datasets:

dict1 = {'a':1, 'b':2, 'c':3}
dict2 = {'a':1, 'b':2, 'c':4}

In this case, I want the output to be something like:

set1 = set([('c', 4), ('c',3)])

as their keys match, but no value.

I tried the option of understanding using the intersection and difference operators, but I can not get the desired result.

Any help is appreciated.

+4
source share
8 answers

If you are using Python 2:

dict1.viewitems() ^ dict2.viewitems()

If you are using Python 3:

dict1.items() ^ dict2.items()

viewitems(Python 2) and items(Python 3) return an object of type set, which we can use the carriage operator to calculate the symmetric difference.

+9
source
set(dict1.items()).symmetric_difference(dict2.items())

Use iteritemsin Python 2 to increase efficiency.

+5

MultiDict. python, boltons. .

from boltons.dictutils import MultiDict

dict1 = {'a':1, 'b':2, 'c':3}
dict2 = {'a':1, 'b':2, 'c':4}

m = MultiDict()
for k in dict1.keys():
    if dict1.get(k) != dict2.get(k):
        m.add(k, dict1.get(k))
        m.add(k, dict2.get(k))

print m
for k in m.keys():
    print k, m.getlist(k)

# OrderedMultiDict([('c', 3), ('c', 4)])
# 'c' [3, 4]
+2

Python. - , , , , .

(, ) .

l = [(k,v,k,dict2[k]) for k,v in dict1 if k in dict2]
+1

; :

dict1 = {'a':1, 'b':2, 'c':3}
dict2 = {'a':1, 'b':2, 'c':4}

set1 = set([(k, v) for k, v in dict1.items()])
set2 = set([(k, v) for k, v in dict2.items()])
diff_set = list(set1 - set2) + list(set2-set1)
print diff_set

:

[('c', 3), ('c', 4)]

:

dict1 = {'a':1, 'b':2, 'c':3}
dict2 = {'a':1, 'b':2, 'c':4}

set1 = set(dict1.items())
set2 = set(dict2.items())
diff_set = set1 ^ set2
print diff_set
+1
 for key, val in dict1.iteritems():
   if key in dict2 and val != dict2[key]:
     set1.add((key, val))

 for key, val in dict2.iteritems():
   if key in dict1 and val != dict1[key]:
     set1.add((key, val))
+1

:

for value in zip(dict1.iteritems(), dict2.iteritems()):
    if(value[0] != value[1]):
        tuple = value
print tuple

(('c', 3), ('c', 4))

+1

set symmetric_difference:

set(dict1.items()) ^ set(dict2.items())

edit. Python 2, timeit.

The fastest . set(dict1.viewitems()).symmetric_difference(dict2.viewitems())

Complete the second, and the most readable will be dict1.viewitems() ^ dict2.viewitems()

Worse is my answer : set(dict1.items()) ^ set(dict2.items())

>>> from timeit import timeit

>>> setup = ('dict1 = {str(i): i for i in range(1000)}; '
...          'dict2 = {str(i): (i if i % 10 else i - 1) for i in range(1000)}')

This gives us two dictionaries with elements 1000and 10%different ones, i.e. 200symmetric difference:

>>> exec(setup)
>>> len(dict1.viewitems() ^ dict2.viewitems())
200

We are going to check every case 30,000 times:

>>> def check(expression):
...     return timeit(expression, setup, number=30000)

Timings best worst:

>>> check('set(dict1.viewitems()).symmetric_difference(dict2.viewitems())')
8.233164442241105

>>> check('dict1.viewitems() ^ dict2.viewitems()')
8.242523450809585

>>> check('set(dict1.viewitems()).symmetric_difference(dict2.items())')
8.651751725357371

>>> check('set(dict1.items()).symmetric_difference(dict2.items())')
8.774394999897368

>>> check('set(dict1.items()) ^ set(dict2.items())')
9.795530728021276
+1
source

All Articles