2015-06-05 155 views
0

我有兩個字典:如何合併兩個字典的值?

{ 
'1234': 
    { 
     '2015-05-31T00:00:00.000Z': 795, 
     '2015-05-21T00:00:00.000Z': 985, 
     '2015-05-29T00:00:00.000Z': 805, 
     '2015-05-28T00:00:00.000Z': 955, 
     '2015-06-04T00:00:00.000Z': 1365, 
     '2015-05-24T00:00:00.000Z': 845, 
     '2015-06-03T00:00:00.000Z': 1545, 
     '2015-05-23T00:00:00.000Z': 825, 
     '2015-05-30T00:00:00.000Z': 875, 
     '2015-05-22T00:00:00.000Z': 1165, 
     '2015-05-27T00:00:00.000Z': 1065, 
     '2015-06-01T00:00:00.000Z': 1145, 
     '2015-06-05T00:00:00.000Z': 625, 
     '2015-05-20T00:00:00.000Z': 745, 
     '2015-06-02T00:00:00.000Z': 1405, 
     '2015-05-26T00:00:00.000Z': 1255, 
     '2015-05-25T00:00:00.000Z': 1135 
    } 
} 

{ 
'1234': 
    { 
     '2015-05-31T00:00:00.000Z': 794, 
     '2015-05-21T00:00:00.000Z': 980, 
     '2015-05-29T00:00:00.000Z': 802, 
     '2015-06-02T00:00:00.000Z': 1400, 
     '2015-05-26T00:00:00.000Z': 1256, 
     '2015-05-25T00:00:00.000Z': 1138 
    } 
} 

如何合併這兩個類型的字典與以下,如果一個鍵值項存在,合併這兩個值?

+0

合併的邏輯是什麼?你想要做什麼以及到目前爲止你嘗試過什麼? – locoboy

+0

你的預期結果是什麼?你有什麼嘗試? –

+0

你確定這些是詞典?這看起來不像em ... – Zizouz212

回答

1

您可以通過使用更新

dict_a.update(dict_b) 

這將覆蓋從那裏有重疊鍵dict_b值dict_a添加值dict_b到dict_a,而且還添加任何新的密鑰從dict_b。

如果你想把這個在第三字典,不影響前兩則先複製字典

dict_c = dict_a.copy() 
dict_c.update(dict_b) 
1

你可以通過字典循環,如果你想將它們合併。不知道這是否是你最好的方式。

dict1 = {'1234': {'2015-05-31T00:00:00.000Z': 795, '2015-05-21T00:00:00.000Z': 985, '2015-05-29T00:00:00.000Z': 805, '2015-05-28T00:00:00.000Z': 955, '2015-06-04T00:00:00.000Z': 1365, '2015-05-24T00:00:00.000Z': 845, '2015-06-03T00:00:00.000Z': 1545, '2015-05-23T00:00:00.000Z': 825, '2015-05-30T00:00:00.000Z': 875, '2015-05-22T00:00:00.000Z': 1165, '2015-05-27T00:00:00.000Z': 1065, '2015-06-01T00:00:00.000Z': 1145, '2015-06-05T00:00:00.000Z': 625, '2015-05-20T00:00:00.000Z': 745, '2015-06-02T00:00:00.000Z': 1405, '2015-05-26T00:00:00.000Z': 1255, '2015-05-25T00:00:00.000Z': 1135}} 
dict2 = {'1234': {'2015-05-31T00:00:00.000Z': 794, '2015-05-21T00:00:00.000Z': 980, '2015-05-29T00:00:00.000Z': 802, '2015-06-02T00:00:00.000Z': 1400, '2015-05-26T00:00:00.000Z': 1256, '2015-05-25T00:00:00.000Z': 1138}} 
dict3 = {'1234': {}} 

for key, value in dict1['1234'].iteritems(): 
    if key in dict2['1234'].keys(): 
     dict3['1234'][key] = value + dict2['1234'][key] 
    else: 
     dict3['1234'][key] = value 

輸出:

{ 
    '1234': 
     { 
     '2015-05-31T00:00:00.000Z': 1589, 
     '2015-05-26T00:00:00.000Z': 2511, 
     '2015-05-21T00:00:00.000Z': 1965, 
     '2015-05-22T00:00:00.000Z': 1165, 
     '2015-05-27T00:00:00.000Z': 1065, 
     '2015-06-01T00:00:00.000Z': 1145, 
     '2015-05-28T00:00:00.000Z': 955, 
     '2015-06-05T00:00:00.000Z': 625, 
     '2015-06-02T00:00:00.000Z': 2805, 
     '2015-06-04T00:00:00.000Z': 1365, 
     '2015-05-20T00:00:00.000Z': 745, 
     '2015-05-23T00:00:00.000Z': 825, 
     '2015-05-24T00:00:00.000Z': 845, 
     '2015-05-30T00:00:00.000Z': 875, 
     '2015-05-25T00:00:00.000Z': 2273, 
     '2015-06-03T00:00:00.000Z': 1545, 
     '2015-05-29T00:00:00.000Z': 1607 
     } 
} 

Apperently還有那些已經模塊。根據https://docs.python.org/2/library/collections.html;

0

如果你正在尋找的值加在一起,其中密鑰匹配:

new_dict = {} 

for key in dict_2.keys(): 
    new_dict[key] = {} 
    for subkey in dict_2[key]: 
     if subkey in dict_1[key]: 
      result = dict_1[key][subkey] + dict_2[key][subkey] 
      new_dict[key][subkey] = result 

print(new_dict) 

輸出:

{ 
    '1234': 
     { 
      '2015-05-31T00:00:00.000Z': 1589, 
      '2015-05-26T00:00:00.000Z': 2511, 
      '2015-05-21T00:00:00.000Z': 1965, 
      '2015-06-02T00:00:00.000Z': 2805, 
      '2015-05-25T00:00:00.000Z': 2273, 
      '2015-05-29T00:00:00.000Z': 1607 
     } 
} 
1

這可能是另一種解決方案:

from itertools import chain 
z = dict(chain(dict_a.iteritems(), dict_b.iteritems())) 

這可能得分性能優於copy解決方案。

1

我不確定你的意思是「合併」。如果合併它們,相同的鍵值將被覆蓋。就個人而言,我會將字典轉換爲字符串,然後做一些更改並將所有內容都轉換爲字典與literal_eval。它工作得很好。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import ast 

d1 ={'1234':{'2015-05-31T00:00:00.000Z':795}} 
d2 ={'1234':{'2015-05-31T00:00:00.000Z':794}} 

s=str(d1)+str(d2) 
s=s.replace('}{', ',') 

d3=ast.literal_eval(s) 

print d3 

輸出:

{'1234': {'2015-05-31T00:00:00.000Z': 794}} 

如果這是你的意思。