By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Comparing value in two dictionaries?

P: n/a
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.
Nov 15 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 15 '08 #2

P: n/a
Gilles Ganault <no****@nospam.comwrites:
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]]

--
Arnaud
Nov 15 '08 #3

P: n/a
Arnaud Delobelle wrote:
Gilles Ganault <no****@nospam.comwrites:
>Hello

I fill two dictionaries with the same number of keys, and then need to
compare the value for each key, ...
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)
>>differs
{'h': (104, 13), 'e': (101, 12)}
--Scott David Daniels
Sc***********@Acm.Org
Nov 15 '08 #4

P: n/a
Arnaud Delobelle <ar*****@googlemail.comwrites:
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!

--
Arnaud
Nov 15 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.