473,545 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing value in two dictionaries?

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.nom bre et verif.ape.nombr e
import apsw

#============
dic1={}
[...]
rows=list(curso r.execute(sql))
for id in rows:
dic1[id[0]] = id[1]
#============
dic2={}
[...]
rows=list(curso r.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
4 5954
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
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.nom bre et verif.ape.nombr e
import apsw

#============
dic1={}
[...]
rows=list(curso r.execute(sql))
for id in rows:
dic1[id[0]] = id[1]
#============
dic2={}
[...]
rows=list(curso r.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
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.ite ms()))
if p1 != p2)
>>differs
{'h': (104, 13), 'e': (101, 12)}
--Scott David Daniels
Sc***********@A cm.Org
Nov 15 '08 #4
Arnaud Delobelle <ar*****@google mail.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
8639
by: Kill Bill | last post by:
type(i) == "<type 'float'>" this always returns false. How come? type(i)returns <type 'float'> if i is a float so why isn't == working?
19
2628
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor = Color.Empty then..... or if mycolor is Color.Empty then .......
1
1477
by: François Pinard | last post by:
Hi, people. I noticed today that dictionaries seem to support `==' comparison. (Retrospectively, it is strange that I never needed it before! :-) Yet, before relying on this, I seeked for confirmation in the Python manuals, and did not succeed in finding it. In particular: http://www.python.org/doc/2.3.5/lib/typesmapping.html is...
19
3812
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that...
11
11402
by: John Salerno | last post by:
I'd like to compare the values in two different sets to test if any of the positions in either set share the same value (e.g., if the third element of each set is an 'a', then the test fails). I have this: def test_sets(original_set, trans_letters): for pair in zip(original_set, trans_letters): if pair == pair: return False
8
13283
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more dictionaries which themselves may contain ints, None and more dictionaries. Each of the sub-dictionaries is also arbitrarily large. When pretty...
2
3371
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
5
2070
by: brad | last post by:
I want to compare two dicts that should have identical info just in a different data structure. The first dict's contents look like this. It is authoritative... I know for sure it has the correct key value pairs: {'001' : '01'} The second dict's contents are like this with a tuple instead of a string for the key: {('This is one',...
0
128
by: Jon Bowlas | last post by:
Many thanks for all your reponses, much appreciated. I'll get back to you on which is the best for me. BTW - yes John thats exactly what I wanted. Cheers Jon
0
7496
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5354
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.