从 python 字典中删除值
removing values from a python dictionary
早上好。我有一些嵌套字典,每个键有多个项目。我只需要提取包含只有 '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'}]}
从字面上将您的字典列表过滤为恰好有两个键的字典,以及您感兴趣的两个键值。
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 = {
# 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)
使用dictkeys
函数可以得到dict中的key
然后将其与 {"ans", "val"}
进行比较以过滤掉不需要的字典。
for key in dict_i_have.keys():
dict_i_have[key] = [item for item in dict_i_have[key] if set(item.keys()) == {'ans', 'val'}]
尝试下面的简单解决方案,
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)
早上好。我有一些嵌套字典,每个键有多个项目。我只需要提取包含只有 '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'}]}
从字面上将您的字典列表过滤为恰好有两个键的字典,以及您感兴趣的两个键值。
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 = {
# 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)
使用dictkeys
函数可以得到dict中的key
然后将其与 {"ans", "val"}
进行比较以过滤掉不需要的字典。
for key in dict_i_have.keys():
dict_i_have[key] = [item for item in dict_i_have[key] if set(item.keys()) == {'ans', 'val'}]
尝试下面的简单解决方案,
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)