2017-05-25 97 views
0

我目前有兩個字典。我想採用包含更多鍵 - 值對的第二個字典,並將字典值附加到第一個字典中的值。如何將兩個字典作爲值(二級字典)結合使用

Dictionary1 = {'47289': {'totcount': 5.0, 'totunits': 89.8}, 
       '18109': {'totcount': 82.92, 'totunits': 829.67}} 

Dictionary2 = {'47289': {'average': 927.90, 'median': 288.8}, 
       '18109': {'average': 927.92, 'median': 3728.97}, 
       '92738': {'average': 729.91, 'median': 472.88}} 

Result = {'47289': {'totcount': 5.0, 'totunits': 89.8, 'average': 927.90, 'median': 288.8}, 
      '18109': {'totcount': 82.92, 'totunits': 829.67, 'average': 927.92, 'median': 3728.97}} 

,當我嘗試使用Python update()功能:

Dictionary1.update(Dictionary2) 

結果是第一個字典,第二庫值和第一字典中的值已被刪除。

Dictionary1 = {'47289': {'average': 927.90, 'median': 288.8}, 
       '18109': {'average': 927.92, 'median': 3728.97}} 
+1

不完全是。 OP想要合併二級字典。 – DyZ

回答

0

這應該工作:

for key, value in Dictionary1.iteritems(): 
    value.update(Dictionary2[key]) 

(這是你需要更新的嵌套字典。)

+0

工作完美!謝謝! –