I have two dictionaries:
{
'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
}
}
and
{
'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
}
}
How can I merge these two dicts with following and if a key-value item is existing, merge the two value?
答案 0 :(得分:1)
您可以使用update
将dict_b添加到dict_a中dict_a.update(dict_b)
这将用dict_b中的值覆盖dict_a中的值,其中有重叠的键,并且它还会添加来自dict_b的任何新键。
如果你想把它放在第三个字典中,不影响前两个字典,那么先复制字典
dict_c = dict_a.copy()
dict_c.update(dict_b)
答案 1 :(得分:1)
如果要合并它们,可以遍历dict。不确定这是否是你最好的方式。
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
}
}
显然已经有了一个模块。根据{{3}};
答案 2 :(得分:1)
这可能是另一种解决方案:
from itertools import chain
z = dict(chain(dict_a.iteritems(), dict_b.iteritems()))
与copy
解决方案相比,这在性能方面得分更高。
答案 3 :(得分: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}}
如果这就是你的意思。
答案 4 :(得分: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
}
}