Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Comparing value in two dictionaries?

Reply
Thread Tools

Comparing value in two dictionaries?

 
 
Gilles Ganault
Guest
Posts: n/a
 
      11-15-2008
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.
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      11-15-2008
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
 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      11-15-2008
Gilles Ganault <> writes:

> 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
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      11-15-2008
Scott David Daniels <> writes:

> Arnaud Delobelle wrote:
>> Gilles Ganault <> writes:
>>
>>> 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)}


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)

--
Arnaud
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      11-15-2008
Arnaud Delobelle <> writes:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strange outcome when comparing int value against a give minimum and maximum value Pugi! Javascript 2 02-12-2007 09:10 PM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Comparing two textboxes Steffen Loringer ASP .Net 1 04-26-2004 09:55 AM
warning - comparing a signed value to an unsinged value Kevin Goodsell C Programming 30 10-22-2003 12:12 PM
comparing pointed-at value with another value Pierre McCann C Programming 6 10-10-2003 10:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57