473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

comparing dictionaries

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', '001'): '01'}

Pseudo Code:
for key, value in first_dict.iteritems():
# How do I do the following line?
if key not in second_dict or if it is, but has has the wrong value,
then let me know

Jun 27 '08 #1
5 2063
On May 7, 4:08 pm, brad <byte8b...@gmail.comwrote:
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', '001'): '01'}

Pseudo Code:
for key, value in first_dict.iteritems():
# How do I do the following line?
if key not in second_dict or if it is, but has has the wrong value,
then let me know
Well

for k, v in first_dict.items():
if k not in second_dict or k in second_dict and k[v] !=
second_dict[k]:
let you know?
Jun 27 '08 #2
brad wrote:
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', '001'): '01'}
It looks like extracting key[1] from each key of the second dictionary
should yield the keys of the first dictionary. If that is the case, then
the following should do it:

d1 = {'001':'01', '002':'02'}
d2 = {('This is one', '001'): '01', ('This is two', '002'): '02'}

if d1 == dict( zip( (k[1] for k in d2.keys()), d2.values() ) ):
print "They're 'equal'."

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 27 '08 #3
brad <by*******@gmail.comwrites:
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'}
-refdict
>
The second dict's contents are like this with a tuple instead of a
string for the key:

{('This is one', '001'): '01'}
-multidict
>
Pseudo Code:
for key, value in first_dict.iteritems():
# How do I do the following line?
if key not in second_dict or if it is, but has has the wrong value,
then let me know
I think it's best to iterate over items of the second dictionary
first. I am assuming that a key can be repeated in the second
dictionary, otherwise it could be a bit simpler:

missing_keys = set(first_dict)
for keys, val in second_dict.iteritems():
for key in keys:
missing_keys.discard(key)
if first_dict.get(key, val) != val:
print "wrong value for", key, 'in', keys
if missing_keys:
print 'some keys are missing:', ',',join(remaining)

--
Arnaud
Jun 27 '08 #4
Hello,
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', '001'): '01'}

Pseudo Code:
for key, value in first_dict.iteritems():
* *# How do I do the following line?
* *if key not in second_dict or if it is, but has has the wrong value,
then let me know
def cmp_dicts(first, second):
return sorted(first.iteritems()) == \
sorted(((k[1], v) for k, v in second.iteritems()))

Google for DSU (Decorate-Sort-Undecorate)

HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com
Jun 27 '08 #5

"Miki" <mi*********@gmail.comwrote in message
news:ad**********************************@b9g2000p rh.googlegroups.com...
Hello,
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', '001'): '01'}

Pseudo Code:
for key, value in first_dict.iteritems():
# How do I do the following line?
if key not in second_dict or if it is, but has has the wrong value,
then let me know
Does this work for you?

for key,value in second_dict.iteritems():
try:
if first_dict[key(1)] == value:
return True
else:
return False:
except KeyError:
return False

tjr

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
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?
0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
8
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
3
by: Shivram U | last post by:
Hi, I want to store dictionaries on disk. I had a look at a few modules like bsddb, shelve etc. However would it be possible for me to do the following hash = where the key is an int and not...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
1
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...
19
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...
8
by: placid | last post by:
Hi all, Just wondering if anyone knows how to pop up the dialog that windows pops up when copying/moving/deleting files from one directory to another, in python ? Cheers
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
4
by: Gilles Ganault | last post by:
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.