簡體   English   中英

用字典鍵替換另一個字典中的值

[英]Replacing dictionary keys for values in another dictionary

我有一個詞典列表,其中包含我想在每個詞典的鍵下替換的信息。

因此,我正在考慮遍歷每個字典並替換值。

我還有另一本字典,將要替換的值作為鍵,將最終值替換為值。

它是這樣的:

listOfDicts = [{'value_1' : 'A', 'value-2' : 'B', 'valu&_3' : 'C'}, {'value-1' : 'D', 'value_2' : 'E', 'value-3' : 'F'}, {'value_1' : 'G', 'value_2' : 'H', 'value_3' : 'I'}]

然后,我還有另一本字典可以用作修復此信息的基礎:

fixer = {'value-2' : 'value_2', 'value&_3' : 'value_3', 'value-3' : 'value_3', ...}

我該如何更換?

-編輯:

所需的輸出將是這樣的:

listOfDicts = [{
'value_1' : 'A',
'value_2' : 'B',
'value_3' : 'C'},
{'value_1' : 'D',
'value_2' : 'E',
'value_3' : 'F'},
...}

我們可以使用以下功能修復每本詞典:

fixer = {'value-2' : 'value_2', 'value&_3' : 'value_3', 'value-3' : 'value_3'}

def fix_dictionary(dict_):
    return { fixer.get(k, k): v for k, v in dict_.items() }

對於給定的dict_這將構造一個新的字典,其中fixer中的鍵將被“固定”(由fixer詞典中的相應值替換)。

然后,我們可以使用以下方法生成新的固定詞典列表:

[fix_dictionary(dict_) for dict_ in listOfDicts]

或者我們可以消除該功能,而只使用一個襯里:

[{ fixer.get(k, k): v for k, v in dict_.items() } for dict_ in listOfDicts]

這應該為您工作:

listOfDicts = [{'value_1' : 'A', 'value-2' : 'B', 'value&_3' : 'C'}, {'value-1' : 'D', 'value_2' : 'E', 'value-3' : 'F'}, {'value_1' : 'G', 'value_2' : 'H', 'value_3' : 'I'}]
fixer = {'value-1' : 'value_1', 'value-2' : 'value_2', 'value&_3' : 'value_3', 'value-3' : 'value_3'}

for i in range(len(listOfDicts)):

    keys = list(listOfDicts[i].keys())
    temp_dict = dict()

    for item in keys:
        if item in fixer:
            temp_dict[fixer[item]] = listOfDicts[i][item]

        else:
            temp_dict[item] = listOfDicts[i][item]

    listOfDicts[i] = temp_dict
print(listOfDicts)

輸出量

[{'value_1': 'A', 'value_2': 'B', 'value_3': 'C'}, {'value_1': 'D', 'value_2': 'E', 'value_3': 'F'}, {'value_1': 'G', 'value_2': 'H', 'value_3': 'I'}]


說明

該程序的作用是迭代列表中的每個項目,並在當前項目的字典中獲取鍵。 然后,它創建一個臨時字典( temp_dict ),稍后將在其中存儲固定值。 然后,程序經過,看到任何按鍵是否不需要固定的,如果是這樣,它修復基礎上的按鍵fixer字典。 最后,它將用固定值替換listOfDicts要迭代的項。

如果我正確理解了,您想要這樣的東西嗎?

import json
import copy


listOfDicts = [
    {
        "valu&_3": "C",
        "value-2": "B",
        "value_1": "A"
    },
    {
        "value-1": "D",
        "value-3": "F",
        "value_2": "E"
    },
    {
        "value_1": "G",
        "value_2": "H",
        "value_3": "I"
    }
]
fixer = {
    "valu&_3": "value_3",
    "value-2": "value_2",
    "value-3": "value_3"
}

newDict = copy.deepcopy(listOfDicts)
for oldDct in newDict:
    for k2, v2 in fixer.items():
        value = oldDct.pop(k2, None)
        if value:
            oldDct[v2] = value

print('listOfDicts'.center(80, '-'))
print(json.dumps(listOfDicts, indent=4))
print('newDict'.center(80, '-'))
print(json.dumps(newDict, indent=4))

輸出:

----------------------------------listOfDicts-----------------------------------
[
    {
        "valu&_3": "C",
        "value-2": "B",
        "value_1": "A"
    },
    {
        "value-1": "D",
        "value-3": "F",
        "value_2": "E"
    },
    {
        "value_1": "G",
        "value_2": "H",
        "value_3": "I"
    }
]
------------------------------------newDict-------------------------------------
[
    {
        "value_1": "A",
        "value_3": "C",
        "value_2": "B"
    },
    {
        "value-1": "D",
        "value_2": "E",
        "value_3": "F"
    },
    {
        "value_1": "G",
        "value_2": "H",
        "value_3": "I"
    }
]

暫無
暫無

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

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