| | |
Combining Multiple Element Dictionaries
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
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!
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)
>>> x=nested_dict() >>> y=nested_dict() >>> >>> x[1][1]=1 >>> x[1][2]=2 >>> y[1][3]=3 >>> x.update(y) >>> print x defaultdict(<class 'nested_dict.nested_dict'>, {1: defaultdict(<class 'nested_dict.nested_dict'>, {3: 3})})
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
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.
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
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
Thanks!
the 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
the
Python Syntax (Toggle Plain Text)
x[1].update(y[1])
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
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
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.
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)
x = {} x[1] = {} x[1][1]=1 x[1][2]=2 y = {} y[2] = {} y[2][3]=3 x.update(y) print x # # prints {1: {1: 1, 2: 2}, 2: {3: 3}}
Last edited by woooee; Sep 16th, 2009 at 1:13 am.
Linux counter #99383
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
This is what I ended up with, works great 

Python Syntax (Toggle Plain Text)
def merge(lib1,lib2): for item in lib2.keys(): lib1[item].update(lib2[item]) return lib1
Last edited by chase32; Sep 16th, 2009 at 1:22 pm.
•
•
•
•
This is what I ended up with, works great
Python Syntax (Toggle Plain Text)
def merge(lib1,lib2): for item in lib2.keys(): lib1[item].update(lib2[item]) return lib1
Here is a typical eaxmple ...
python Syntax (Toggle Plain Text)
# merge two dictionaries and handle key collisions with # collections.defaultdict(dict) import collections def merge(lib1, lib2): for item in lib2.keys(): lib1[item].update(lib2[item]) return lib1 d1 = {'a':{1:11}, 'b':{2:22}, 'c':{3:33}} d2 = {'d':{4:44}, 'c':{5:55}, 'e':{6:66}} # mutliple values go into a dictionary lib1 = collections.defaultdict(dict) lib2 = collections.defaultdict(dict) # convert each dict to a defaultdict lib1.update(d1) lib2.update(d2) print(lib1) print(lib2) print('-'*60) print( merge(lib1, lib2) ) """my result (notice key 'c')--> defaultdict(<type 'dict'>, {'a': {1: 11}, 'c': {3: 33}, 'b': {2: 22}}) defaultdict(<type 'dict'>, {'c': {5: 55}, 'e': {6: 66}, 'd': {4: 44}}) ------------------------------------------------------------ defaultdict(<type 'dict'>, {'a': {1: 11}, 'c': {3: 33, 5: 55}, 'b': {2: 22}, 'e': {6: 66}, 'd': {4: 44}}) """
Last edited by vegaseat; Sep 16th, 2009 at 2:25 pm.
May 'the Google' be with you!
![]() |
Similar Threads
Other Threads in the Python Forum
- Previous Thread: How can I hand off variables from web form to python?
- Next Thread: search file for string
Views: 670 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for dictionary, python
abrupt accessdenied advanced aliased apax avogadro beginner c++ calculator calling code convert copy corners csv cturtle curves development dictionaries dictionary edit enter event examples excel file ftp function google gui images import input ip itunes jaunty java launcher linux list lists maze microsoft mouse movingimageswithpygame mysqldb newb news obexftp opensource path program programming projects py2exe pygame pyglet pyopengl pyqt python rails random read recursion recursive redirect return ruby rubyconf script search server shebang silverlight simple slicenotation smtp sqlite string strip sum syntax table tennis text threading tkinter tlapse tooltip tutorial ubuntu unicode urllib urllib2 variable ventrilo web-scrape wordgame words wxpython







