我有一个包含内部列表和dict的dict列表,我需要将它们连接在一个大dict中。 列表如下:
[{'total': {'last_day': '7'}},
{'total': {'progress': '07/04'}},
{'total': {'piecies': '3008'}},
{'total': {'week': ['16.0']}},
{'total': {'week': ['17.0']}},
{'total': {'week': ['15.0']}},
{'total': {'week': ['17.0']}},
{'total': {'week': ['16.0']}},
{'total': {'week': ['13.0']}},
{'total': {'week': ['6.0']}},
{'tkts': [{'tktvalue': '13.5'}]},
{'tkts': [{'month': {'consuntivato_pezzi': '2346'}}]},
{'tkts': [{'month': {'consuntivato_euro': '31671.00'}}]},
{'tkts': [{'month': {'preventivato_pezzi': '9897'}}]}
]
我尝试了一些for循环和递归函数,但没有很好的结果
for vars in temporary_var:
for title in vars:
try:
try:
table_var[title].update(vars[title])
except KeyError:
table_var[title] = vars[title]
except AttributeError:
table_var[title].append(vars[title][0])
但我只能得到:
{'total': {'last_day': '7', 'progress': '07/04', 'volumes': '3008', 'week': ['6.0']}, 'tkts': [{'service': 'SOSPC'}, {'tktvalue': '13.5'}, {'month': {'volumes1': '2346'}}, {'month': {'volumes2': '31671.00'}}, {'month': {'volumes3': '98
97'}}]}
但我需要这个:
{'total': {'last_day': '7', 'progress': '07/04', 'volumes': '3008', 'week': ['16.0', '17.0', '15.0', '17.0', '16.0', '13.0','6.0']}, 'tkts': [{'service': 'SOSPC', 'tktvalue': '13.5', 'month': {'volumes1': '2346', 'volumes2': '31671.00', 'volumes3': '9897'}}]}
尝试在此处使用默认列表。您可以进一步了解它们collections.defaultdict
输出:
试试这个,它会得到你想要的输出:
输出: {'total':{'last\u day':'7','progress':'07/04','piecies':'3008','week':['16.0','17.0','15.0','17.0','16.0','13.0','6.0']},'tkts':[{'tktvalue':'13.5','month':{'consuntivato\u pezzi':'2346','consuntivato\u euro':'31671.00','preventivato\u pezzi':'9897'}}]
相关问题 更多 >
编程相关推荐