Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重命名字典的键_Python_Dictionary - Fatal编程技术网

Python 重命名字典的键

Python 重命名字典的键,python,dictionary,Python,Dictionary,如何更改Python字典中的键 例程返回字典。 这本字典一切正常,只是有几把钥匙需要重新命名。下面的代码将字典条目(key=value)复制到具有所需键的新条目中,然后删除旧条目。是否有一种更具python风格的方式,或许不需要重复价值 my_dict = some_library.some_method(scan) my_dict['qVec'] = my_dict['Q'] my_dict['rVec'] = my_dict['R'] del my_dict['Q'], my_dict['R

如何更改Python字典中的键

例程返回字典。 这本字典一切正常,只是有几把钥匙需要重新命名。下面的代码将字典条目(key=value)复制到具有所需键的新条目中,然后删除旧条目。是否有一种更具python风格的方式,或许不需要重复价值

my_dict = some_library.some_method(scan)
my_dict['qVec'] = my_dict['Q']
my_dict['rVec'] = my_dict['R']
del my_dict['Q'], my_dict['R']
return my_dict

dict
键是不可变的。这意味着它们无法改变。你可以从网站上读到更多

字典由键索引,键可以是任何不可变类型

下面是一个使用

>>> d = {1:'a',2:'b'}
>>> d[3] = d.pop(1)
>>> d
{2: 'b', 3: 'a'}