How to combine two nested dictionaries under the same dictionary
for example I have a dictionary:
dictA={"nest1":{"01feb":[1,2,3,4,5],"02feb":[1,7,8,9,10]},
"nest2":{"01feb":[1,2,3,4,5],"02feb":[6,4,8,10,10]}}
the list inside is the same length. I need to combine nest1 and nest2 as one dictionary and the result should be like this:
dictA={"nest":{"01feb":[2,4,6,8,10],"02feb":[7,11,16,19,20]}}
source to share
Plese will find the code below for your request.
dictA={"nest1":{"01feb":[1,2,3,4,5],"02feb":[1,7,8,9,10]},
"nest2":{"01feb":[1,2,3,4,5],"02feb":[6,4,8,10,10]}}
result ={}
final_op = {}
for k,v in dictA.iteritems():
for nk,nv in v.iteritems():
if result.has_key(nk):
i=0
while i < len(result[nk]):
result[nk][i] += nv[i]
i += 1
else:
result[nk] = nv
final_op['nest'] = result
print final_op
Output:
{'nest': {'02feb': [7, 11, 16, 19, 20], '01feb': [2, 4, 6, 8, 10]}}
source to share
You can use dict comprehension
, map()
and zip()
like this example (works with Python 2 and Python 3).
dictA = {'nest1': {'01feb': [1, 2, 3, 4, 5], '02feb': [1, 7, 8, 9, 10]},
'nest2': {'01feb': [1, 2, 3, 4, 5], '02feb': [6, 4, 8, 10, 10]}}
a = (v.items() for _, v in map(list, dictA.items()))
# You can also use another map():
# final = {'nest': {k: list(map(sum, zip(v,j))) for (k, v), (_, j) in zip(*a)}}
final = {'nest': {k: [m+n for m, n in zip(v, j)] for (k, v), (_, j) in zip(*a)}}
print(final)
Output:
{'nest': {'02feb': [7, 11, 16, 19, 20], '01feb': [2, 4, 6, 8, 10]}}
source to share
You need to loop over the dict and update the value on iteration.
dictA={"nest1":{"01feb":[1,2,3,4,5],"02feb":[1,7,8,9,10]},
"nest2":{"01feb":[1,2,3,4,5],"02feb":[6,4,8,10,10]}}
def merge(dictA):
merge_dict = {}
for key in dictA:
for sub_key in dictA[key]:
if sub_key in merge_dict:
# update the nested value
merge_dict[sub_key] = [sum(x) for x in zip(*[merge_dict[sub_key], dictA[key][sub_key]])]
else:
merge_dict[sub_key] = dictA[key][sub_key]
return merge_dict
merge_dict = merge(dictA)
dictA.clear()
dictA["nest"] = merge_dict
print(dictA)
source to share
Here is the function you are doing:
def merge_nested(dictionary):
result = dict()
for nested in dictionary.values():
for key in nested.keys():
if key in result:
# We've already found it, add our values to it's
for i in range(len(result[key])):
result[key][i] += nested[key][i]
else:
result[key] = nested[key]
return {"nest":result}
With this function, you will get the following output:
>>> print(merge_nested({"nest1":{"01feb":[1,2,3,4,5],"02feb":[1,7,8,9,10]},"nest2":{"01feb":[1,2,3,4,5],"02feb":[6,4,8,10,10]}}))
{'nest': {'01feb': [2, 4, 6, 8, 10], '02feb': [7, 11, 16, 19, 20]}}
This is a modified version of @ Arockia's answer here
source to share
Solution with groupby
of itertools
:
from itertools import chain, groupby
import operator
dictA={"nest1":{"01feb":[1,2,3,4,5],"02feb":[1,7,8,9,10]},
"nest2":{"01feb":[1,2,3,4,5],"02feb":[6,4,8,10,10]}}
A = {
"nest": dict(
(key, list(map(operator.add, *(v for _, v in group))))
for key, group in groupby(
sorted(
chain(*(v.iteritems() for v in dictA.values())) # Python 2
# chain(*(v.items() for v in dictA.values())) # Python 3
),
lambda x: x[0],
)
)
}
print(A)
Result:
{'nest': {'02feb': [7, 11, 16, 19, 20], '01feb': [2, 4, 6, 8, 10]}}
source to share