Python 迭代期间更改了字典键
Python dictionary keys changed during iteration
我有这样的字典:
d={
'McAfee': {'DatDate': '123','DatVersion': '456'},
'AntiVirus': {'AVName': ['Mac Defender', 'Windows Defender'],'AVVersion': ['262144', '393472']}
}
现在我想改变d的key,根据这个映射也改变d的value的key:
mapping_table = {"McAfee":'Mc_table','AntiVirus':'An_table'}
mapping_coulmn = {'DatDate':"Dc_column",'AVName':'Av_column','DatVersion':'Da_column','AVVersion':'AVV_column'}
输出应该是这样的:
d={
'Mc_table': {'Dc_column': '123','Da_column': '456'},
'An_table': {'Av_column': ['Mac Defender', 'Windows Defender'],'AVV_column': ['262144', '393472']}
}
我累的是:
for key in d:
if key in mapping_table:
new_key = mapping_table[key]
d1[new_key]=d.pop(key)
print(d)
它returns一个错误:
RuntimeError: dictionary keys changed during iteration
有哪位朋友可以帮忙吗?万分感谢!
d = {
mapping_table.get(k, k): {
mapping_column.get(kk, kk): vv for kk, vv in v.items()
}
for k, v in d.items()
}
print(d)
打印:
{'Mc_table': {'Dc_column': '123', 'Da_column': '456'},
'An_table': {'Av_column': ['Mac Defender', 'Windows Defender'],
'AVV_column': ['262144', '393472']}}
我有这样的字典:
d={
'McAfee': {'DatDate': '123','DatVersion': '456'},
'AntiVirus': {'AVName': ['Mac Defender', 'Windows Defender'],'AVVersion': ['262144', '393472']}
}
现在我想改变d的key,根据这个映射也改变d的value的key:
mapping_table = {"McAfee":'Mc_table','AntiVirus':'An_table'}
mapping_coulmn = {'DatDate':"Dc_column",'AVName':'Av_column','DatVersion':'Da_column','AVVersion':'AVV_column'}
输出应该是这样的:
d={
'Mc_table': {'Dc_column': '123','Da_column': '456'},
'An_table': {'Av_column': ['Mac Defender', 'Windows Defender'],'AVV_column': ['262144', '393472']}
}
我累的是:
for key in d:
if key in mapping_table:
new_key = mapping_table[key]
d1[new_key]=d.pop(key)
print(d)
它returns一个错误:
RuntimeError: dictionary keys changed during iteration
有哪位朋友可以帮忙吗?万分感谢!
d = {
mapping_table.get(k, k): {
mapping_column.get(kk, kk): vv for kk, vv in v.items()
}
for k, v in d.items()
}
print(d)
打印:
{'Mc_table': {'Dc_column': '123', 'Da_column': '456'},
'An_table': {'Av_column': ['Mac Defender', 'Windows Defender'],
'AVV_column': ['262144', '393472']}}