Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Compare two nested dictionaries

Reply
Thread Tools

Compare two nested dictionaries

 
 
targetsmart
Guest
Posts: n/a
 
      07-25-2010
Hi,
I am trying to compare two nested dictionaries, I want to know what is
the exact difference between them. I tried this solution

....
s1 = set(result1)
s2 = set(result2)
print s1 - s2

but it doesn't seem show any difference, but

assert result1 == result2
fails

could someone help me to find out the difference the two nested
dictionaries.

Any help is greatly appreciated.

Thanks,
Vivek.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-25-2010
On Sun, 25 Jul 2010 08:03:06 -0700, targetsmart wrote:

> Hi,
> I am trying to compare two nested dictionaries, I want to know what is
> the exact difference between them. I tried this solution
>
> ...
> s1 = set(result1)
> s2 = set(result2)
> print s1 - s2
>
> but it doesn't seem show any difference, but
>
> assert result1 == result2
> fails
>
> could someone help me to find out the difference the two nested
> dictionaries.


Have you tried printing them and just looking for the differences?

Calling set() on a dictionary will create a set from the keys only:

>>> d1 = {"a": 1, "b": 2}
>>> d2 = {"a": 1, "b": 999}
>>> set(d1) == set(d2)

True
>>> d1 == d2

False

If you want to know the difference between two dictionaries, you have to
consider:

(1) Keys that are in the first dict, but not the second;

(2) Keys that are in the second dict, but not the first; and

(3) Keys which are in both dicts, but have different values.


--
Steven
 
Reply With Quote
 
 
 
 
News123
Guest
Posts: n/a
 
      07-25-2010
Hi,

On 07/25/2010 06:21 PM, Steven D'Aprano wrote:
> On Sun, 25 Jul 2010 08:03:06 -0700, targetsmart wrote:
>
>> Hi,
>> I am trying to compare two nested dictionaries, I want to know what is
>> the exact difference between them. I tried this solution
>>
>> ...
>> s1 = set(result1)
>> s2 = set(result2)
>> print s1 - s2
>>


I think you want to have the symmetric difference:
try s1 ^ s2

>> but it doesn't seem show any difference, but
>>

 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      07-25-2010

[targetsmart]
> > I am trying to compare two nested dictionaries, I want to know what is
> > the exact difference between them. I tried this solution


[Steven D'Aprano]
> If you want to know the difference between two dictionaries, you have to
> consider:
>
> (1) Keys that are in the first dict, but not the second;
>
> (2) Keys that are in the second dict, but not the first; and
>
> (3) Keys which are in both dicts, but have different values.


Steven, thanks for the excellent specification. Here's the code:

s1 = set(d1)
s2 = set(d2)
first_not_second = s1 - s2
second_not_first = s2 - s1
difference_values = set(k for k in s1 & s2 if d1[k] != d2[k])

If the values are hashable, an alternate approach is:

s1 = set(d1.items())
s2 = set(d2.items())
first_not_second = s1 - s2
second_not_first = s2 - s1


Raymond
 
Reply With Quote
 
John Nagle
Guest
Posts: n/a
 
      07-26-2010
On 7/25/2010 8:03 AM, targetsmart wrote:
> Hi,
> I am trying to compare two nested dictionaries, I want to know what is
> the exact difference between them.



d1 = {'a' : 1, 'b' : 2, 'c': 3 }
d2 = {'a' : 1, 'b' : 3, 'd': 4 }

diff = dict(set(d1.items()) - set(d2.items()))

print (diff)

{'c': 3, 'b': 2}

That's the true "difference", with all entries in d1 not
identically in d2 listed. Is that what you wanted?

John Nagle

 
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
compare dictionaries Baba Python 9 09-07-2010 11:17 PM
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
dictionaries with nested lists Alex Martelli Python 6 11-02-2003 09:27 AM



Advertisments