Python - 从嵌套字典中删除 K 值键
有时,在处理记录时,我们可能会遇到一个问题,即我们需要从嵌套字典中删除一个键,该键的值是我们特定于 K 的。这是一个常见问题,它在数据域(如 Web 开发)中得到应用。让我们讨论可以执行此任务的某些方式。
Input : test_dict = {‘CS’: {‘priceless’: 6}, ‘is’: {‘better’: 6}, ‘gfg’: {‘best’: 6}}
Output : {‘CS’: {}, ‘gfg’: {}, ‘is’: {}}
Input : test_dict = {‘CS’: {‘priceless’: 9}, ‘is’: {‘better’: 8}, ‘gfg’: {‘best’: 7}}
Output : {‘CS’: {‘priceless’: 9}, ‘is’: {‘better’: 8}, ‘gfg’: {‘best’: 7}}
方法 #1:使用循环 + isinstance() + filter()
上述功能的组合可以用来解决这个问题。在此,我们使用 filter() 执行 K 值的任务,并使用 isinstance() 来测试嵌套字典。字典构造是使用循环完成的。
# Python3 code to demonstrate working of
# Remove K valued key from Nested Dictionary
# Using loop + isinstance() + filter()
# initializing dictionary
test_dict = {'gfg' : {'best' : 4, 'good' : 5},
'is' : {'better' : 6, 'educational' : 4},
'CS' : {'priceless' : 6}}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# initializing rem_val
rem_val = 6
# Remove K valued key from Nested Dictionary
# Using loop + isinstance() + filter()
def rem_vals(ele):
global rem_val
key, val = ele
return val != rem_val
res = dict()
for key, val in test_dict.items():
if isinstance(val, dict):
res[key] = dict(filter(rem_vals, val.items()))
else:
res[key] = val
# printing result
print("Dictionary after removal : " + str(res))
The original dictionary : {‘is’: {‘educational’: 4, ‘better’: 6}, ‘gfg’: {‘best’: 4, ‘good’: 5}, ‘CS’: {‘priceless’: 6}}
Dictionary after removal : {‘is’: {‘educational’: 4}, ‘gfg’: {‘best’: 4, ‘good’: 5}, ‘CS’: {}}
方法#2:使用字典理解 + isinstance()
+ lamda
上述功能的组合可用于使用 lambda函数在一个班轮中执行此任务。
# Python3 code to demonstrate working of
# Remove K valued key from Nested Dictionary
# Using dictionary comprehension + isinstance() + lamda
# initializing dictionary
test_dict = {'gfg' : {'best' : 4, 'good' : 5},
'is' : {'better' : 6, 'educational' : 4},
'CS' : {'priceless' : 6}}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# initializing rem_val
rem_val = 6
# Remove K valued key from Nested Dictionary
# Using dictionary comprehension + isinstance() + lamda
fnc = lambda sub: { key1: fnc(val1) if isinstance(val1, dict) else val1
for key1, val1 in sub.items() if val1 != rem_val}
res = fnc(test_dict)
# printing result
print("Dictionary after removal : " + str(res))
The original dictionary : {‘is’: {‘educational’: 4, ‘better’: 6}, ‘gfg’: {‘best’: 4, ‘good’: 5}, ‘CS’: {‘priceless’: 6}}
Dictionary after removal : {‘is’: {‘educational’: 4}, ‘gfg’: {‘best’: 4, ‘good’: 5}, ‘CS’: {}}