943,822 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1841
  • Python RSS
Sep 15th, 2009
0

Combining Multiple Element Dictionaries

Expand Post »
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!
Python Syntax (Toggle Plain Text)
  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})})
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chase32 is offline Offline
5 posts
since Sep 2009
Sep 15th, 2009
0

Re: Combining Multiple Element Dictionaries

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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Sep 16th, 2009
0

Re: Combining Multiple Element Dictionaries

Thanks!

the
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chase32 is offline Offline
5 posts
since Sep 2009
Sep 16th, 2009
0

Re: Combining Multiple Element Dictionaries

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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Sep 16th, 2009
0

Re: Combining Multiple Element Dictionaries

Marked as solved. Thanks for the help!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chase32 is offline Offline
5 posts
since Sep 2009
Sep 16th, 2009
0

Re: Combining Multiple Element Dictionaries

This is what I ended up with, works great
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chase32 is offline Offline
5 posts
since Sep 2009
Sep 16th, 2009
0

Re: Combining Multiple Element Dictionaries

Click to Expand / Collapse  Quote originally posted by chase32 ...
This is what I ended up with, works great
Python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How can I hand off variables from web form to python?
Next Thread in Python Forum Timeline: search file for string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC