将字典列表中的每个字典键重新映射到不同键名的python方法是什么?新名称必须是现有键和列表的值(list的值+"-"+键)之间的连接。,对于相同的索引。例如,
字典列表:
[[{'Capture & Acquis.': '','Storage & Accounting': 'X','Transformation': ''}],
[{'Process': 'Acquisition','Report': 'Final'}],
[{'Responsible': 'APE','Department': 'ACC'}]]
要添加的名称列表:
['Dataflow','Scope','Owner']
最终输出
[[{'Dataflow-Capture': '','Dataflow-Storage': 'X','Dataflow-Transformation': ''}],
[{'Scope-Process': 'Acquisition','Scope-Report': 'Final'}],
[{'Owner-Responsible': 'APE','Owner-Department': 'ACC'}]]
尝试:
lst_a = [
[
{
"Capture & Acquis.": "",
"Storage & Accounting": "X",
"Transformation": "",
}
],
[{"Process": "Acquisition", "Report": "Final"}],
[{"Responsible": "APE", "Department": "ACC"}],
]
lst_b = ["Dataflow", "Scope", "Owner"]
for l, prefix in zip(lst_a, lst_b):
l[0] = {f"{prefix}-{k.split()[0]}": v for k, v in l[0].items()}
print(lst_a)
打印:
[
[
{
"Dataflow-Capture": "",
"Dataflow-Storage": "X",
"Dataflow-Transformation": "",
}
],
[{"Scope-Process": "Acquisition", "Scope-Report": "Final"}],
[{"Owner-Responsible": "APE", "Owner-Department": "ACC"}],
]