我正在尝试从列表中删除嵌套字典中的元素。因此,我有一个列表,其中有一个字典作为其元素之一,然后又有一个字典作为其键值之一。我知道我将在最上面的列表和最下面的字典中使用的键,而“中间”字典中的键是可变的。如果底部字典中的值不等于某个值,我想删除“中间”字典中的元素。
我认为我需要三个嵌套的for循环才能进入每个字典,但无法弄清楚如何删除“中间”字典中的元素。
到目前为止,我目前的尝试:
#remove unneccessary elements
def getCleanResults(the_dict1):
for elem1 in the_dict1:
the_dict2 = elem['dict1_key']
for key, elem2 in the_dict2.keys():
the_dict3 = elem2[key]
for elem3 in the_dict3:
if 'keyWanted' in elem3:
del elem2
return the_dict
词典列表的模板有:
a = {
{'key1':{'key1.1': {'delete': True, 'result': None},
'key1.2': {'delete': False, 'result': None}},'key2': 3},
{'key1':{'key1.1': {'delete': False, 'result': None},
'key1.2': {'delete': True, 'result': None}},'key2': 5}
}
我要删除键keyx.x
下的键'delete'
,即True
,根据上面的示例,该键将产生:
a = {
{'key1':{'key1.2': {'delete': False, 'result': None}},'key2': 3},
{'key1':{'key1.1': {'delete': False, 'result': None}},'key2': 5}
}
如上例所示,它们删除的键为key1.1
和key1.2
,因此是可变的。
答案 0 :(得分:0)
我想,您的努力来自于python限制,以便在迭代过程中修改字典。要创建所需的内容,您需要复制一份字典-复制,迭代和删除原始密钥;或者或遍历原始密钥,然后将“过滤的”密钥放入新密钥。以后可以用
b = {k1: {k2: v2 for k2, v2 in v1.items() if not v2['delete']}
for k1, v1 in a.items()}
编辑:由于某种原因,您提供的代码不一致,以上解决方案假设结构实际上是
a = {'key1.1': {'key1.1.1': {'delete': True, 'loremipsum': 'perhaps'},
'key1.1.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key1.2': {'key1.2.1': {'delete': False, 'loremipsum': 'perhaps'},
'key1.2.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key2.1': {'key2.1.1': {'delete': False, 'loremipsum': 'perhaps'},
'key2.1.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key2.2': {'key2.2.1': {'delete': False, 'loremipsum': 'perhaps'},
'key2.2.2': {'delete': True, 'loremipsum': 'perhaps'}}
}
b = {'key1.1': {'key1.1.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key1.2': {'key1.2.1': {'delete': False, 'loremipsum': 'perhaps'},
'key1.2.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key2.1': {'key2.1.1': {'delete': False, 'loremipsum': 'perhaps'},
'key2.1.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key2.2': {'key2.2.1': {'delete': False, 'loremipsum': 'perhaps'}}
}
答案 1 :(得分:0)
如果对字典a
使用正确的语法,则可以迭代第一层密钥,然后像您所说的那样,进行嵌套循环以迭代第二层密钥。 the_dict1 [key1] [key2]中的VALUE是最终的字典。我使用pprint帮助可视化字典。
def getCleanResults(the_dict1):
# iterate through the first keys, cast as a tuple to make a copy of the keys iterator (allows for changing dictionary size during iteration)
for key1 in tuple(the_dict1.keys()):
the_dict2 = the_dict1[key1]
# iterate through the second tier keys
for key2 in tuple(the_dict2.keys()):
if the_dict1[key1][key2]['delete']:
del the_dict1[key1][key2]
# no need to return a new dict, it was passed by reference, we modified the original dict. If you wanted a copy of the dict, make copy and return it instead.
正确格式的词典:
a = {
'key1.1': {'key1.1.1': {'delete': True, 'loremipsum': 'perhaps'},
'key1.1.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key1.2': {'key1.2.1': {'delete': False, 'loremipsum': 'perhaps'},
'key1.2.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key2.1': {'key2.1.1': {'delete': False, 'loremipsum': 'perhaps'},
'key2.1.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key2.2': {'key2.2.1': {'delete': False, 'loremipsum': 'perhaps'},
'key2.2.2': {'delete': True, 'loremipsum': 'perhaps'}}
}
测试:
from pprint import pprint
getCleanResults(a)
print()
pprint(a)
输出:
{'key1.1': {'key1.1.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key1.2': {'key1.2.1': {'delete': False, 'loremipsum': 'perhaps'},
'key1.2.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key2.1': {'key2.1.1': {'delete': False, 'loremipsum': 'perhaps'},
'key2.1.2': {'delete': False, 'loremipsum': 'perhaps'}},
'key2.2': {'key2.2.1': {'delete': False, 'loremipsum': 'perhaps'}}}
答案 2 :(得分:0)
您可以尝试这样。
代码段
import json
def delete_items(d):
keys1 = d.keys()
new_d = {}
for key1 in keys1:
keys2 = d[key1].keys();
for key2 in keys2:
if type(d[key1][key2]) is dict:
if d[key1][key2]["delete"] == False:
if key1 in new_d:
new_d[key1][key2] = d[key1][key2]
else:
new_d[key1] = {key2: d[key1][key2]}
else:
if key1 in d:
new_d[key1] = {key2: d[key1][key2]}
else:
new_d[key1][key2] = d[key1][key2]
return new_d
a = {
"key1.1": {
"key1.1.1": {
"delete": True,
"loremipsum": "perhaps"
},
"key1.1.2": {
"delete": False,
"loremipsum": "perhaps"
},
"key1.1.3": 4
},
"key1.2": {
"key1.2.1": {
"delete": False,
"loremipsum": "perhaps"
},
"key1.2.2": {
"delete": False,
"loremipsum": "perhaps"
}
},
"key2.1": {
"key2.1.1": {
"delete": False,
"loremipsum": "perhaps"
},
"key2.1.2": {
"delete": False,
"loremipsum": "perhaps"
},
"key2.1.3": 9
},
"key2.2": {
"key2.2.1": {
"delete": False,
"loremipsum": "perhaps"
},
"key2.2.2": {
"delete": True,
"loremipsum": "perhaps"
},
"key2.2.3": 2
}
}
d = delete_items(a)
print(json.dumps(d, indent=4))
输出
{
"key1.2": {
"key1.2.1": {
"delete": false,
"loremipsum": "perhaps"
},
"key1.2.2": {
"delete": false,
"loremipsum": "perhaps"
}
},
"key1.1": {
"key1.1.3": 4,
"key1.1.2": {
"delete": false,
"loremipsum": "perhaps"
}
},
"key2.1": {
"key2.1.3": 9
},
"key2.2": {
"key2.2.3": 2
}
}