Combining Multiple Element Dictionaries

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 4
Reputation: chase32 is an unknown quantity at this point 
Solved Threads: 0
chase32 chase32 is offline Offline
Newbie Poster

Combining Multiple Element Dictionaries

 
0
  #1
Sep 15th, 2009
I am trying to combine two multi-element dictionaries but am running into a problem.

From the example below, what I expected was the dictionary x to contain all three unique combinations.

What seems to have happened is that the y dictionary overwrote the x dictionary.

Am I just using this wrong? Is there a elegant way to add two multi element dictionaies?
thanks!
  1. >>> x=nested_dict()
  2. >>> y=nested_dict()
  3. >>>
  4. >>> x[1][1]=1
  5. >>> x[1][2]=2
  6. >>> y[1][3]=3
  7. >>> x.update(y)
  8. >>> print x
  9. defaultdict(<class 'nested_dict.nested_dict'>, {1: defaultdict(<class 'nested_dict.nested_dict'>, {3: 3})})
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Combining Multiple Element Dictionaries

 
0
  #2
Sep 15th, 2009
This is correct, as you are updating x[1], that is y's key is also 1 and so overwrite's whatever is in x for that key. Change to
y[2][3]=3
or use
x[1].update(y[1])
and you should get what you expect.. If that's not satisfactory then you will have to write your own routine to update however you want.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: chase32 is an unknown quantity at this point 
Solved Threads: 0
chase32 chase32 is offline Offline
Newbie Poster

Re: Combining Multiple Element Dictionaries

 
0
  #3
Sep 16th, 2009
Thanks!

the
  1. x[1].update(y[1])
format should work.

If I walk through the keys of Y and use the same value for the key of X, that should let me merge two arbitrary two element dictionaries
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Combining Multiple Element Dictionaries

 
0
  #4
Sep 16th, 2009
Indeed it should, but test for "key in x" as well, or use a try/except, as you can never tell. Please mark this "solved" so no wastes their time on a solved thread.

Edit: I just did the following and it worked fine so you don't have to test for "key in x" as update takes care of that for you.
  1. x = {}
  2. x[1] = {}
  3. x[1][1]=1
  4. x[1][2]=2
  5.  
  6. y = {}
  7. y[2] = {}
  8. y[2][3]=3
  9. x.update(y)
  10. print x
  11. #
  12. # prints
  13. {1: {1: 1, 2: 2}, 2: {3: 3}}
Last edited by woooee; Sep 16th, 2009 at 1:13 am.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: chase32 is an unknown quantity at this point 
Solved Threads: 0
chase32 chase32 is offline Offline
Newbie Poster

Re: Combining Multiple Element Dictionaries

 
0
  #5
Sep 16th, 2009
Marked as solved. Thanks for the help!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: chase32 is an unknown quantity at this point 
Solved Threads: 0
chase32 chase32 is offline Offline
Newbie Poster

Re: Combining Multiple Element Dictionaries

 
0
  #6
Sep 16th, 2009
This is what I ended up with, works great
  1. def merge(lib1,lib2):
  2. for item in lib2.keys():
  3. lib1[item].update(lib2[item])
  4. return lib1
Last edited by chase32; Sep 16th, 2009 at 1:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Combining Multiple Element Dictionaries

 
0
  #7
Sep 16th, 2009
Originally Posted by chase32 View Post
This is what I ended up with, works great
  1. def merge(lib1,lib2):
  2. for item in lib2.keys():
  3. lib1[item].update(lib2[item])
  4. return lib1
Mild caveat, for this to work lib1 and lib2 have to be of type collections.defaultdict(dict) , and each value has to be a dictionary too.

Here is a typical eaxmple ...
  1. # merge two dictionaries and handle key collisions with
  2. # collections.defaultdict(dict)
  3.  
  4. import collections
  5.  
  6. def merge(lib1, lib2):
  7. for item in lib2.keys():
  8. lib1[item].update(lib2[item])
  9. return lib1
  10.  
  11. d1 = {'a':{1:11}, 'b':{2:22}, 'c':{3:33}}
  12. d2 = {'d':{4:44}, 'c':{5:55}, 'e':{6:66}}
  13.  
  14. # mutliple values go into a dictionary
  15. lib1 = collections.defaultdict(dict)
  16. lib2 = collections.defaultdict(dict)
  17.  
  18. # convert each dict to a defaultdict
  19. lib1.update(d1)
  20. lib2.update(d2)
  21.  
  22. print(lib1)
  23. print(lib2)
  24. print('-'*60)
  25. print( merge(lib1, lib2) )
  26.  
  27. """my result (notice key 'c')-->
  28. defaultdict(<type 'dict'>, {'a': {1: 11}, 'c': {3: 33}, 'b': {2: 22}})
  29. defaultdict(<type 'dict'>, {'c': {5: 55}, 'e': {6: 66}, 'd': {4: 44}})
  30. ------------------------------------------------------------
  31. defaultdict(<type 'dict'>, {'a': {1: 11}, 'c': {3: 33, 5: 55},
  32. 'b': {2: 22}, 'e': {6: 66}, 'd': {4: 44}})
  33. """
Last edited by vegaseat; Sep 16th, 2009 at 2:25 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

Tags
dictionary, python

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 670 | Replies: 6
Thread Tools Search this Thread



Tag cloud for dictionary, python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC