I have two dictionaries, both with a nested dictionary, and I want to merge them in a bigger dictionary. Some of the keys are different in the two dictionaries, some of them are the same. The ones that are different, I simply want to copy to the new bigger dictionary. The ones that are the same, I want to keep the key both from the dictionary and the nested one the same, and add the values from the nested dictionary.
Sample data:
dict1 = {'C-STD-B&M-L':
{datetime.date(2015, 4, 30): 0.06875104,
datetime.date(2015, 5, 1): 0.07842368,
datetime.date(2015, 5, 2): 0.08919679999999999,
datetime.date(2015, 5, 16): 0.40798848},
'G-CAM-MAS-XXS':
{datetime.date(2015, 4, 30): 0.0008190299999999999,
datetime.date(2015, 5, 1): 0.00093426,
datetime.date(2015, 5, 2): 0.0010626}
dict2 = {'C-STD-B&M-L':
{datetime.date(2015, 5, 16): 0.075968,
datetime.date(2015, 5, 17): 0.086656},
'H-QLD-BAC-STD':
{datetime.date(2015, 5, 16): 0.17804999999999999,
datetime.date(2015, 5, 17): 0.2031,
datetime.date(2015, 5, 18): 0.23099999999999998}
Expected output:
new_dict =
{'C-STD-B&M-L':
{datetime.date(2015, 4, 30): 0.06875104,
datetime.date(2015, 5, 1): 0.07842368,
datetime.date(2015, 5, 2): 0.08919679999999999,
datetime.date(2015, 5, 16): 0.40798848 + 0.075968
datetime.date(2015, 5, 17): 0.086656},
'G-CAM-MAS-XXS':
{datetime.date(2015, 4, 30): 0.0008190299999999999,
datetime.date(2015, 5, 1): 0.00093426,
datetime.date(2015, 5, 2): 0.0010626
'H-QLD-BAC-STD':
{datetime.date(2015, 5, 16): 0.17804999999999999,
datetime.date(2015, 5, 17): 0.2031,
datetime.date(2015, 5, 18): 0.23099999999999998}
I posted the value as a sum (0.40798848 + 0.075968) just to clarify, I actually need the sum (0.48395648).
I tried to do some for loops to add them but they got really messy, I'll post just for the sake of it.
Sample code:
all_unis_dict = {}
for skus1, subdict1 in unsw_mid_year_dict.items():
for skus2, subdict2 in unsw_mid_year_dict.items():
for dates1, days1 in subdict1.items():
for dates2, days2 in subdict2.items():
pprint(days1 + days2)
Anyone know a elegant way to do it?