Python 从字典的嵌套列表中删除特定键

Python 从字典的嵌套列表中删除特定键,python,recursion,Python,Recursion,我有包含复杂字典的列表的特定格式&再次包含嵌套格式的字典列表,例如 要求是从所有关联字典中删除问题id options = [ { "value": 1, "label": "Paints", "question_id": "207", "question": "Which Paint Brand?", "question_type_id": 2, "options": [

我有包含复杂字典的列表的特定格式&再次包含嵌套格式的字典列表,例如

要求是从所有关联字典中删除问题id

options = [
    {
        "value": 1,
        "label": "Paints",
        "question_id": "207",
        "question": "Which Paint Brand?",
        "question_type_id": 2,
        "options": [
            {
                "value": 2,
                "label": "Glidden",
                "question": "Is it Glidden Paint?",
                "question_id": 1,
                "options": [{"question_id": 1,"value": 10000, "label": "No"}, {"question_id": 1,"value": 10001, "label": "Yes"}],
            },
            {
                "value": 1,
                "label": "Valspar",
                "question": "Is it Valspar Paint?",
                "question_id": 1,
                "options": [{"question_id": 1,"value": 10000, "label": "No"}, {"question_id": 1,"value": 10001, "label": "Yes"}],
            },
            {
                "value": 3,
                "label": "DuPont",
                "question": "Is it DuPont Paint?",
                "question_id": 1,
                "options": [{"question_id": 1,"value": 10000, "label": "No"}, {"question_id": 1,"value": 10001, "label": "Yes"}],
            },
        ],
    },
    {
        "value": 4,
        "label": "Rods",
        "question": "Which Rods Brand?",
        "question_id": 2,
        "options": [
            {"value": 3, "label": "Trabucco"},
            {"value": 5, "label": "Yuki"},
            {"value": 1, "label": "Shimano"},
            {"value": 4, "label": "Daiwa"},
            {"value": 2, "label": "Temple Reef"},
        ],
    },
    {
        "value": 3,
        "label": "Metal Sheets",
        "question": "Which Metal Sheets Brand?",
        "question_id": 2,
        "options": [
            {"value": 2, "label": "Nippon Steel Sumitomo Metal Corporation"},
            {"value": 3, "label": "Hebei Iron and Steel Group"},
            {"value": 1, "label": "ArcelorMittal"},
        ],
    },
    {
        "value": 2,
        "label": "Door Knobs Locks",
        "question": "Which Door Knobs Locks Brand?",
        "question_id": 2,
        "options": [
            {
                "value": 1,
                "label": "ASSA-Abloy",
                "question": "Is it ASSA-Abloy Door Knobs Locks?",
                "question_type_id": 1,
                "options": [{"value": 10000, "label": "No"}, {"value": 10001, "label": "Yes"}],
            },
            {
                "value": 4,
                "label": "RR Brink",
                "question": "Is it RR Brink Door Knobs Locks?",
                "question_type_id": 1,
                "options": [{"value": 10000, "label": "No"}, {"value": 10001, "label": "Yes"}],
            },
            {
                "value": 3,
                "label": "Medeco",
                "question": "Is it Medeco Door Knobs Locks?",
                "question_type_id": 1,
                "options": [{"value": 10000, "label": "No"}, {"value": 10001, "label": "Yes"}],
            },
            {
                "value": 2,
                "label": "Evva",
                "question": "Is it Evva Door Knobs Locks?",
                "question_type_id": 1,
                "options": [{"value": 10000, "label": "No"}, {"value": 10001, "label": "Yes"}],
            },
        ],
    },
]
为此,我编写了一段代码&尝试递归地运行它

from collections import MutableMapping


def delete_keys_from_dict(dictionary_list, keys):
    keys_set = set(keys)  # Just an optimization for the "if key in keys" lookup.
    # modified_list=[]
    for index, dictionary in enumerate(dictionary_list):
        modified_dict = {}
        for key, value in dictionary.items():
            if key not in keys_set:
                if isinstance(value, list):
                    modified_dict[key] = delete_keys_from_dict(value, keys_set)
                else:
                    if isinstance(value, MutableMapping):
                        modified_dict[key] = delete_keys_from_dict(value, keys_set)
                    else:
                        modified_dict[key] = value
                        # or copy.deepcopy(value) if a copy is desired for non-dicts.
        dictionary_list[index] = modified_dict
    return dictionary_list
它返回的列表不正确&这并没有保留现有的列表数据


我可以知道,我哪里出了问题,或者在什么地方遗漏了什么吗?

我想这样的事情应该可以满足你的要求

obj可以是任何对象,它递归到列表和dict中

def delete_keys(obj, keys):
    if isinstance(obj, list):
        return [
            delete_keys(item, keys)
            for item in obj
        ]
    if isinstance(obj, dict):
        return {
            key: delete_keys(value, keys)
            for (key, value) in obj.items()
            if key not in keys
        }
    return obj  # Nothing to do for this value
e、 g

输出

[{'label': 'Paints',
  'options': [{'label': 'Glidden',
               'options': [{'label': 'No', 'value': 10000},
                           {'label': 'Yes', 'value': 10001}],
               'question': 'Is it Glidden Paint?',
               'value': 2}],
  'question': 'Which Paint Brand?',
  'question_type_id': 2,
  'value': 1},
 {'label': 'Rods',
  'options': [{'label': 'Trabucco', 'value': 3},
              {'label': 'Yuki', 'value': 5},
              {'label': 'Shimano', 'value': 1},
              {'label': 'Daiwa', 'value': 4},
              {'label': 'Temple Reef', 'value': 2}],
  'question': 'Which Rods Brand?',
  'value': 4}]

这到底应该做什么?您期望的输出是什么?我想从嵌套列表中关联的所有字典中删除quesiton\u id。代码做什么而不是删除question\u id?你得到的结果有什么不对?注意,delete_keys_from_dict似乎应该是List[dict]类型,但最内层的if分支将普通dict传递给它;但是,乍一看,您的数据似乎没有这种情况。您正在进行isinstance检查,并且在断言您的值是一个列表之后,您正在使用该值调用delete_keys_from_dict。所以你把一份清单当作一份口述。这可能会引起问题。谢谢你。让我看看你的解决方案。
[{'label': 'Paints',
  'options': [{'label': 'Glidden',
               'options': [{'label': 'No', 'value': 10000},
                           {'label': 'Yes', 'value': 10001}],
               'question': 'Is it Glidden Paint?',
               'value': 2}],
  'question': 'Which Paint Brand?',
  'question_type_id': 2,
  'value': 1},
 {'label': 'Rods',
  'options': [{'label': 'Trabucco', 'value': 3},
              {'label': 'Yuki', 'value': 5},
              {'label': 'Shimano', 'value': 1},
              {'label': 'Daiwa', 'value': 4},
              {'label': 'Temple Reef', 'value': 2}],
  'question': 'Which Rods Brand?',
  'value': 4}]