文章目录
字典key 重命名, 直接上码
Python 3.8.13 (default, Mar 28 2022, 06:16:26)
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mydict = {"old_key": "old_value"}
>>> mydict['new_key']=mydict.pop['old_key']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not subscriptable
>>> mydict['new_key']=mydict.pop('old_key')
>>> mydict
{'new_key': 'old_value'}
>>>
结论: 巧妙使用pop函数