Comparing value in two dictionaries?

G

Gilles Ganault

Hello

I fill two dictionaries with the same number of keys, and then need to
compare the value for each key, eg.

#Pour chaque APE, comparaison societe.ape.nombre et verif.ape.nombre
import apsw

#============
dic1={}
[...]
rows=list(cursor.execute(sql))
for id in rows:
dic1[id[0]] = id[1]
#============
dic2={}
[...]
rows=list(cursor.execute(sql))
for id in rows:
dic2[id[0]] = id[1]
#============
#Here, compare each key/value to find values that differ
for i in dic1.items():
[...]

What would be a good way to do this in Python?

Thank you.
 
B

bearophileHUGS

Gilles Ganault:
I fill two dictionaries with the same number of keys, and then need to
compare the value for each key, eg.

Probably using the DBMS capabilities you can find a better solution.

Are the keys equal? If you want to do it using dicts, you can iterate
on one dict, with iteritems, and then using the key look for the value
of the second dict, to see if they are the same, to collect
differences, etc.

Bye,
bearophile
 
A

Arnaud Delobelle

Gilles Ganault said:
Hello

I fill two dictionaries with the same number of keys, and then need to
compare the value for each key, eg.

#Pour chaque APE, comparaison societe.ape.nombre et verif.ape.nombre
import apsw

#============
dic1={}
[...]
rows=list(cursor.execute(sql))
for id in rows:
dic1[id[0]] = id[1]
#============
dic2={}
[...]
rows=list(cursor.execute(sql))
for id in rows:
dic2[id[0]] = id[1]
#============
#Here, compare each key/value to find values that differ
for i in dic1.items():
[...]

What would be a good way to do this in Python?

Thank you.

I think you would be better off writing some SQL query that does it.

If you want to do this in Python, I suppose it depends whether you know
that the two dictionaries have the same set of keys. If they do you can
simply write something like:

diff = [key for key, val1 in dic1.iteritems() if val1 != dic2[key]]
 
A

Arnaud Delobelle

Scott David Daniels said:
if you know set(dic1) == set(dic2) -- that is that the same keys are
used, you could use:
Setup:
dic1 = dict((c, ord(c)) for c in 'abcdefgh')
dic2 = dic1.copy()
dic2['e'] = 12
dic2['h'] = 13
Comparisons:
differs = dict((p1[0], (p1[1], p2[1]))
for p1, p2 in zip(sorted(dic1.items()),
sorted(dic2.items()))
if p1 != p2)
{'h': (104, 13), 'e': (101, 12)}

This may break if two keys are not comparable or if the order on keys is
not total.

Moreover it is O(nlogn) (as sorting is) whereas iterating
over items of one dictionary and looking up the other one will be O(n)
(assuming dictionary lookup is O(1)):

[k for k, v in dic1.iteritems() if dic2[k] != v]

Or to obtain a dictionary of differences:

dict((k, (v, dic2[v]) for k, v in dic1.iteritems()
if dic2[v] != v)
 
A

Arnaud Delobelle

Arnaud Delobelle said:
Or to obtain a dictionary of differences:

dict((k, (v, dic2[v]) for k, v in dic1.iteritems()
if dic2[v] != v)
^
Should be k of course!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top