簡體   English   中英

如何重命名字典中的所有鍵?

[英]How can I rename all keys in a dict?

我想重命名 Python 中的字典鍵。

有兩個鍵,'Curry_Vegetables_Set1 (59).JPG131850'和'Curry_Vegetables_Set1 (62).JPG104359',我想用replace1值重命名它們。 我該如何重命名它們?

以下是字典示例:

file = {'Curry_Vegetables_Set1 (59).JPG131850': {'filename': '1.5_Curry_Vegetables_59.jpg',
  'size': 131850,
  'regions': [{'shape_attributes': {'name': 'polygon',
     'all_points_x': [510, 563,622,],
     'all_points_y': [459,  523, 505,]},
    'region_attributes': {'food': 'curry_vegetable'}}],
  'file_attributes': {}},
 'Curry_Vegetables_Set1 (62).JPG104359': {'filename': '1.5_Curry_Vegetables_62.jpg',
  'size': 104359,
  'regions': [{'shape_attributes': {'name': 'polygon',
     'all_points_x': [471,490,528,],
     'all_points_y': [496,476,493]},
    'region_attributes': {'food': 'curry_vegetable'}}],
  'file_attributes': {}},}

我嘗試了下面的代碼,

for key,value in file.items():
    name = key.split('.')
    num = name[0].split('(')
    image_num = num[1][:-1]
    replace1 = '1.5_Curry_Vegetables_'+image_num+'.'+name[1]

    # replace old keys with replace1
    file[replace1] = file[key]

但它給出的錯誤為:

RuntimeError: dictionary changed size during iteration

你並沒有真正改變舊的鑰匙; 你在同一個字典中添加新的。 您應該創建一個新的空字典並將新的鍵/值對添加到循環中。

file2 = {}
for key, value in file.items():
    name = key.split('.')
    num = name[0].split('(')
    image_num = num[1][:-1]
    replace1 = '1.5_Curry_Vegetables_' + image_num + '.' + name[1]

    # replace old keys with replace1
    file2[replace1] = file[key]
    
print(file2)

到位:

oldKeys = list(dictionary.keys())
for oldKey in oldKeys:
    name = oldKey .split('.')
    num = name[0].split('(')
    image_num = num[1][:-1]
    newKey = '1.5_Curry_Vegetables_' + image_num + '.' + name[1]
    dictionary[newKey] = dictionary.pop(oldKey)

或者通過創建一個新的字典:

def newKeyFromOld(oldKey):
    name = oldKey .split('.')
    num = name[0].split('(')
    image_num = num[1][:-1]
    newKey = '1.5_Curry_Vegetables_' + image_num + '.' + name[1]
    return newKey
{newKeyFromOld(oldKey): value for oldKey, value in dictionary.items()}

獲得RunTimeError的原因是您正在更新已使用file.items()加載的字典。 將字典項轉換為list會創建其項的列表,因此您可以對其進行迭代並避免RunTimeError

file = {'Curry_Vegetables_Set1 (59).JPG131850': {'filename': '1.5_Curry_Vegetables_59.jpg',
    'size': 131850,
    'regions': [{'shape_attributes': {'name': 'polygon', 'all_points_x': [510, 563,622,], 'all_points_y': [459,  523, 505,]},
    'region_attributes': {'food': 'curry_vegetable'}}],
    'file_attributes': {}
},
    'Curry_Vegetables_Set1 (62).JPG104359': {'filename': '1.5_Curry_Vegetables_62.jpg',
    'size': 104359,
    'regions': [{'shape_attributes': {'name': 'polygon', 'all_points_x': [471,490,528,], 'all_points_y': [496,476,493]},
    'region_attributes': {'food': 'curry_vegetable'}}],
  'file_attributes': {}},
}
for key,value in list(file.items()):
    name = key.split('.')
    num = name[0].split('(')  
    image_num = num[1][:-1]   
    replace1 = '1.5_Curry_Vegetables_'+image_num+'.'+name[1] 
    file[replace1] = file[key]
    del file[key]
print (file)

輸出:

{'1.5_Curry_Vegetables_59.JPG131850': {'filename': '1.5_Curry_Vegetables_59.jpg', 'size': 131850, 'regions': [{'shape_attributes': {'name': 'polygon', 'all_points_x': [510, 563, 622], 'all_points_y': [459, 523, 505]}, 'region_attributes': {'food': 'curry_vegetable'}}], 'file_attributes': {}}, '1.5_Curry_Vegetables_62.JPG104359': {'filename': '1.5_Curry_Vegetables_62.jpg', 'size': 104359, 'regions': [{'shape_attributes': {'name': 'polygon', 'all_points_x': [471, 490, 528], 'all_points_y': [496, 476, 493]}, 'region_attributes': {'food': 'curry_vegetable'}}], 'file_attributes': {}}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM