Python字典键变更 [英] Python dictionary key change
本文介绍了Python字典键变更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建某种数据库,我需要更改字典中的键.
I am creating a sort of a database and I need to change a key in a dictionary.
{'lastExecution': '1-1-1970', 'wachtwoord': 'wachtwoord', 'name': {'laatsteUpdate': '1-1-1970', 'puntenAantal': '0'}}
如何更改现在称为name的密钥,并且仍然保留为其分配的数据?
How can I change the key that is now called name and still keep the data assigned to it?
PS,我正在使用python(3.7.0b4)
PS, I am using python(3.7.0b4)
推荐答案
尝试一下:
dct = {'lastExecution': '1-1-1970', 'wachtwoord': 'wachtwoord',
'name': {'laatsteUpdate': '1-1-1970', 'puntenAantal': '0'}}
dct["new_name"] = dct.pop("name")
print(dct)
输出将如下所示:
{'wachtwoord': 'wachtwoord', 'new_name': {'puntenAantal': '0',
'laatsteUpdate': '1-1-1970'}, 'lastExecution': '1-1-1970'}
这篇关于Python字典键变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文