我有包含复杂字典和;再次包含字典列表(嵌套格式),例如
并且要求从所有关联字典中删除question_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
可以是任何对象,这会递归到列表和目录中例如
输出
相关问题 更多 >
编程相关推荐