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?
You can add dict_b into dict_a by using update
dict_a.update(dict_b)
this will overwrite the values in dict_a with the values from dict_b where there are overlapping keys, and it also adds any new keys from dict_b.
If you want to put this in a third dictionary, not affecting the first two then first copy the dictionary
dict_c = dict_a.copy()
dict_c.update(dict_b)
You could loop through dict if you want to merge them. Not sure if that's the best way thou.
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
Output:
{
'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 there's a module for that already. According to https://docs.python.org/2/library/collections.html ;
This could be another solution:
from itertools import chain
z = dict(chain(dict_a.iteritems(), dict_b.iteritems()))
This could score better on performance front compared to copy
solution.
I am not sure what you mean by "merge". If you merge them the same keys' values will be overwritten. Personally I would convert dictionaries into strings, then do some changes and convert everything back to dictionary with literal_eval. It works very well.
#!/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
Output:
{'1234': {'2015-05-31T00:00:00.000Z': 794}}
If that is what you mean.
If you're looking to add the values together where keys match:
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)
Output:
{
'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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.