在Python中,我有字典.如何更改此词典的键? [英] In Python, I have a dictionary. How do I change the keys of this dictionary?
本文介绍了在Python中,我有字典.如何更改此词典的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一本非常复杂的字典.
Let's say I have a pretty complex dictionary.
{'fruit':'orange','colors':{'dark':4,'light':5}}
无论如何,我的目标是扫描此复杂的多级词典中的每个键.然后,将"abc"附加到每个键的末尾.
Anyway, my objective is to scan every key in this complex multi-level dictionary. Then, append "abc" to the end of each key.
因此它将是:
{'fruitabc':'orange','colorsabc':{'darkabc':4,'lightabc':5}}
你会怎么做?
推荐答案
键不能更改.您将需要添加一个具有修改后值的新键,然后删除旧键,或者使用dict理解或类似方法创建一个新dict.
Keys cannot be changed. You will need to add a new key with the modified value then remove the old one, or create a new dict with a dict comprehension or the like.
这篇关于在Python中,我有字典.如何更改此词典的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文