comparing dictionaries

B

brad

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
 
C

cokofreedom

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?
 
C

Carsten Haese

brad said:
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,
 
A

Arnaud Delobelle

brad said:
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)
 
M

Miki

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,
 
T

Terry Reedy

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top