请帮帮我:)。
Python 3.5
我有dict列表:
[
{
'A':'string1',
'B':'string_5',
'C':'string_9,
...
'N':'stringN',
'metric1':5,
'metric2':7
},
{
'A':'string1',
'B':'string_5',
'C':'string_11,...
'N':'stringN',
'metric1':10,
'metric2':45
},
{
'A':'string2',
'B':'string_7',
'C':'string_15,
...
'N':'stringN',
'metric1':234,
'metric2':78
},
.......
]
在输出中我想得到:
[
{
'A':'string1', 'metric1':sum(all metric1 where 'A'='string1'),
'metric2':sum(all metric2 where 'A'='string1'),
'B': [
{
'B':'string_5',
'metric1':sum(all metric1 for combination where 'A' in
'string1' and 'B' in string_5),
'metric2':sum(all metric2 , where combination Similarly 'metric1'),
'C':[
{
'C':'string_9', 'metric1':sum(all metric_1 for
combination where 'A' in` 'string1' and 'B' in string_5 and 'C' is 'string_9'),
'metric2':sum(...)
},
{
'C':'string_11',
'metric1':sum(...)
'metric2':sum(..)
}
]
}
]
},
{
'A':'string_2', 'metric1':sum(all metric1 where 'A'='string2'),
'metric2':sum(...),
'B': [
{
'B':'string_7','metric1':sum(all metric1 for
combination where 'A'='string_2' and 'B'='string_7'),
'metric2':'sum(...),
'C': [
{
'C':'string_15',
'metric1' :sum(...),
'metric2':'sum(...)
}
]
}
]
},
....
.....
]
因此,输出必须为每个级别键(A,B,C,... N)分组输入,并对每个分组的所有度量值求和。在每个父级别的dict中,它具有下一级别名称的键,具有下一个分组级别元素的数组值,其中元素的父级是此键。
Аny解决方案?
我尝试调用循环输入dict的递归函数作为树,但是我得到的结果只有一个分支,只有一个分支到下一个分支等
答案 0 :(得分:0)
我的同事帮我找到解决方案。
def generate_level(dicts, level, levels):
level2dicts = defaultdict(list) # level value -> dicts
for d in dicts:
level2dicts[d[level]].append(d)
if not levels: # the deepest level
return {level: [{level: level_value, **sum_metrics(level_dicts)}
for level_value, level_dicts in level2dicts.items()]}
inner_level, *levels = levels # pop level
inner_dicts = (generate_level(level_dicts, inner_level, levels)
for level_dicts in level2dicts.values())
return {level: [{level: level_value,
**sum_metrics(level_dict[inner_level]),
**level_dict}
for level_value, level_dict in zip(level2dicts, inner_dicts)]}
level, *nested_levels = order
print(generate_level(dicts, level, nested_levels)[level])
'为了' - 有序的键列表(级别顺序), '类型的字典' - 输入词典列表
结果:
[{'A': 'string1',
'B': [{'B': 'string_5',
'C': [{'C': 'string_9', 'metric1': 5, 'metric2': 7},
{'C': 'string_11', 'metric1': 10, 'metric2': 45}],
'metric1': 15,
'metric2': 52}],
'metric1': 15,
'metric2': 52},
{'A': 'string2',
'B': [{'B': 'string_7',
'C': [{'C': 'string_15', 'metric1': 234, 'metric2': 78}],
'metric1': 234,
'metric2': 78}],
'metric1': 234,
'metric2': 78}]
感谢所有拒绝我的问题的人! @Rory Daulton