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 has the same length. I need to merge 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]}}
Arockia
Plese find the below code for your query.
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]}}
Chiheb Nexus
You can use a 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]}}
You have to traverse the dict and update the value in the 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)
Here is a function to do what you're asking for:
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 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
A 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]}}
来源:https://stackoverflow.com/questions/44601256/how-to-merge-two-nested-dictionaries-under-a-same-dictionary