python - 如何将字典列表与内部列表连接起来
问题描述
我有一个带有内部列表和字典的字典列表,我需要将它们连接到一个大字典中。清单是这样的:
[{'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'}}]}
解决方案
试试这个,它会得到你想要的输出:
lst=[{'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'}}]}
]
d={} # the dictionary that will hold the result
for dd in lst: # for each dictionary in the list of dictionaries
for key,value in dd.items():
if key not in d: # key does not exist in dictionary d
d[key]=value
else: # key exists in dictionary d
if isinstance(value,dict): # check if the value is a dictionary or a list
for key1,value2 in value.items():
if key1 not in d[key]:
d[key]={**d[key],**value} # combine the dictionaries
else:
d[key][key1].append(value2[0])
elif isinstance(value,list): # check if the value is a list
if isinstance(value[0],dict): # check if the value is a dictionary or a list
for key1,value2 in value[0].items():
if key1 not in d[key][0]:
d[key][0]={**d[key][0],**value[0]}
else:
d[key][0][key1]={**d[key][0][key1],**value2}
print(d)
输出:{'total': {'last_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_pezzi': '2346', 'consuntivato_euro': '31671.00', 'preventivato_pezzi': '9897'}}]}
推荐阅读
- oop - DAO 和 SOLID 设计原则
- amazon-dynamodb - 可查询的未绑定数量
- sql-server - 如何使用 SQL Server OPENQUERY 一次删除 1000 行
- discord.py - Discord Bot 发送多条消息
- swift - 使用 NetworkExtension API 在 macOS 中实现 Web 过滤器
- php - Laravel Nova:向 Laravel Nova 资源添加行号(非 ID)
- javascript - 寻求建议如何处理这个嵌套的 JSON/Object
- c - 为什么 `volatile` 变量的值不会改变?
- python - 在Python2.7的函数中独立执行代码块
- javascript - 页面加载时调用 Javascript onclick 函数