I have a list of dicts stored in the list. I want to merge the same dicts in to one.
Input:
data = [
{'company': 'DAZ', 'process': 'Acesses', 'task': 'security', 'cmp_hours': 5106, 'prc_hours':744, 'hours':6},
{'company': 'DAZ', 'process': 'Acesses', 'task': 'Approve', 'cmp_hours': 5106, 'prc_hours':744, 'hours':10},
{'company': 'CAP', 'process': 'Agreements', 'task': 'Check', 'cmp_hours': 400, 'prc_hours':50, 'hours':5},
{'company': 'CAP', 'process': 'Offboarding', 'task': 'Formation', 'cmp_hours': 400, 'prc_hours':75, 'hours':7},
{'company': 'TOO', 'process': 'Offboarding', 'task': 'Formation', 'cmp_hours': 200, 'prc_hours':32, 'hours':2}
]
Required Output:
[
{
'DAZ': {'cmp_hours':5106, 'processes': [{'process': 'Acesses', 'prc_hours' : 744,'tasks': [{'task': 'security', 'hours': 6}, {'task': 'Approve', 'hours': 10}]}]},
'CAP': {'cmp_hours':400, 'processes': [{'process': 'Agreements', 'prc_hours' : 50,'tasks': [{'task': 'Check', 'hours': 5}]}, {'process_name': 'Offboarding', 'prc_hours' : 75,'tasks': [{'task': 'Formation', 'hours': 7}]}]},
'TOO': {'cmp_hours':200, 'processes': [{'process': 'Offboarding', 'prc_hours' : 32,'tasks': [{'task': 'Formation', 'hours': 2}]}]}
}
]
Approximate structure
[{company : {cmp_hours, 'processes': [{process, prc_hours, 'tasks':[{task, hours}]}]}}]
CodePudding user response:
You can use collections.defaultdict
with recursion:
from collections import defaultdict
def merge(d, p):
r = defaultdict(list)
for i in d:
r[i[p[0][0]]].append(i)
if p[0][1] is None:
return {a:{p[0][-1]:sum(i[p[0][-1]] for i in b),
**merge(b, p[1:])} for a, b in r.items()}
return {p[0][1]:[{p[0][0]:a, p[0][-1]:sum(i[p[0][-1]] for i in b),
**({} if not p[1:] else merge(b, p[1:]))} for a, b in r.items()]}
data = [{'company': 'DAZ', 'process': 'Acesses', 'task': 'security', 'cmp_hours': 5106, 'prc_hours': 744, 'hours': 6}, {'company': 'DAZ', 'process': 'Acesses', 'task': 'Approve', 'cmp_hours': 5106, 'prc_hours': 744, 'hours': 10}, {'company': 'CAP', 'process': 'Agreements', 'task': 'Check', 'cmp_hours': 400, 'prc_hours': 50, 'hours': 5}, {'company': 'CAP', 'process': 'Offboarding', 'task': 'Formation', 'cmp_hours': 400, 'prc_hours': 75, 'hours': 7}, {'company': 'TOO', 'process': 'Offboarding', 'task': 'Formation', 'cmp_hours': 200, 'prc_hours': 32, 'hours': 2}]
print(merge(data, [['company', None, 'cmp_hours'], ['process', 'processes', 'prc_hours'], ['task', 'tasks', 'hours']]))
Output:
{'DAZ': {'cmp_hours': 10212, 'processes': [{'process': 'Acesses', 'prc_hours': 1488, 'tasks': [{'task': 'security', 'hours': 6}, {'task': 'Approve', 'hours': 10}]}]}, 'CAP': {'cmp_hours': 800, 'processes': [{'process': 'Agreements', 'prc_hours': 50, 'tasks': [{'task': 'Check', 'hours': 5}]}, {'process': 'Offboarding', 'prc_hours': 75, 'tasks': [{'task': 'Formation', 'hours': 7}]}]}, 'TOO': {'cmp_hours': 200, 'processes': [{'process': 'Offboarding', 'prc_hours': 32, 'tasks': [{'task': 'Formation', 'hours': 2}]}]}}