只需2个步骤即可轻松完成:
dictionary[new_key] = dictionary[old_key]del dictionary[old_key]
或第一步:
dictionary[new_key] = dictionary.pop(old_key)
KeyError如果
dictionary[old_key]未定义,它将引发。请注意,这 将 删除
dictionary[old_key]。
>>> dictionary = { 1: 'one', 2:'two', 3:'three' }>>> dictionary['ONE'] = dictionary.pop(1)>>> dictionary{2: 'two', 3: 'three', 'ONE': 'one'}>>> dictionary['ONE'] = dictionary.pop(1)Traceback (most recent call last): File "<input>", line 1, in <module>KeyError: 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)