Compare two nested dictionaries

Discussion in 'Python' started by targetsmart, Jul 25, 2010.

  1. targetsmart

    targetsmart Guest

    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.
     
    targetsmart, Jul 25, 2010
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Jul 25, 2010
    #2
    1. Advertising

  3. targetsmart

    News123 Guest

    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
    >>
     
    News123, Jul 25, 2010
    #3
  4. [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
     
    Raymond Hettinger, Jul 25, 2010
    #4
  5. targetsmart

    John Nagle Guest

    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
     
    John Nagle, Jul 26, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GenxLogic
    Replies:
    3
    Views:
    1,286
    andrewmcdonagh
    Dec 6, 2006
  2. MRAB
    Replies:
    0
    Views:
    114
  3. Mohan L
    Replies:
    0
    Views:
    114
    Mohan L
    Oct 4, 2013
  4. MRAB
    Replies:
    0
    Views:
    109
  5. Tobiah
    Replies:
    0
    Views:
    106
    Tobiah
    Oct 4, 2013
Loading...

Share This Page