从python字典中删除值

2024-10-01 00:32:37 发布

您现在位置:Python中文网/ 问答频道 /正文

早上好。我有一些嵌套字典,每个键有多个项。我只需要提取包含仅具有“ans”和“val”对的项的对,如果该项包含“ans”、“type”、“value”等,则我要将其删除。下面是我拥有的字典示例和预期输出。 欢迎任何建议,非常感谢

数据

dict_i_have = {
    "x19": [
        {
            "ans": "Enter number",
            "type": "number",
            "value": "input_val",
            "validators": [
                {"dype": "int", "invalid": "Enter number between 0 and 100", "min": 0, "max": 100},
                {"vtype": "no", "message": "Please enter a value"},
            ],
        },
        {"ans": "One year or less", "val": "-10"},
        {"ans": "Do not know", "val": "1"},
        {"ans": "Other", "val": "3"},
    ],
    "x20": [
        {
            "ans": "Enter number",
            "type": "number",
            "value": "input_val",
            "validators": [
                {"dype": "int", "invalid": "Enter number between 0 and 50", "min": 0, "max": 50},
                {"vtype": "no", "message": "Please enter a value"},
            ],
        },
        {"ans": "Six months or less", "val": "10"},
    ],
}

预期产出

dict_i_want = {'x19': [{'ans': 'One year or less', 'val': '-10'},
                       {'ans': 'Do not know', 'val': '1'},
                       {'ans': 'Other', 'val': '3'}],
               'x20': [{'ans': 'Six months or less', 'val': '10'}]}

Tags: ornumberinput字典valuetypevaldict
3条回答

您可以通过字典理解来实现这一点:

dict_i_have = {
    # elided for brevity
}

dict_i_want = {
    key: [
        {"ans": q["ans"], "val": q["val"]} for q in questions if "val" in q
    ]
    for (key, questions) in dict_i_have.items()
}
print(dict_i_want)

或者如果你想保留对原始记录的引用,而不是复制到新记录中

dict_i_want = {
    key: [q for q in questions if set(q) == {"ans", "val"}]
    for (key, questions) in dict_i_have.items()
}
print(dict_i_want)

从字面上说,将你的dict列表过滤到那些正好有两个键的dict列表,其中包含你感兴趣的两个键值

dict_i_want = dict()
for key, values in dict_i_have.items():
     subdicts = [d for d in values if len(d) == 2 and 'ans' in d and 'val' in d]
     dict_i_want[key] = subdicts

请尝试以下简单的解决方案

    dict_i_have = {
        "x19": [
            {
                "ans": "Enter number",
                "type": "number",
                "value": "input_val",
                "validators": [
                    {"dype": "int", "invalid": "Enter number between 0 and 100", "min": 0, "max": 100},
                    {"vtype": "no", "message": "Please enter a value"},
                ],
            },
            {"ans": "One year or less", "val": "-10"},
            {"ans": "Do not know", "val": "1"},
            {"ans": "Other", "val": "3"},
        ],
        "x20": [
            {
                "ans": "Enter number",
                "type": "number",
                "value": "input_val",
                "validators": [
                    {"dype": "int", "invalid": "Enter number between 0 and 50", "min": 0, "max": 50},
                    {"vtype": "no", "message": "Please enter a value"},
                ],
            },
            {"ans": "Six months or less", "val": "10"},
        ],
    }
    
# Actual Solution starts here
    for key1 in dict_i_have:
        for val in dict_i_have[key1]:
            if len(val) > 2 :
                dict_i_have[key1].remove(val)
                
    print(dict_i_have)

相关问题 更多 >