Python – 如果给定键的值为 N,则删除字典
给定字典列表,删除键 K 为 N 的字典。
Input : test_list = [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 5, “is” : 4, “Best” : 10}, {“Gfg” : 3, “is” : 6, “Best” : 15}], K = “Gfg”, N = 9
Output : [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 5, “is” : 4, “Best” : 10}, {“Gfg” : 3, “is” : 6, “Best” : 15}]
Explanation : All elements are extracted which have “Gfg” other than 9.
Input : test_list = [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 5, “is” : 4, “Best” : 10}, {“Gfg” : 3, “is” : 6, “Best” : 15}], K = “Best”, N = 10
Output : [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 3, “is” : 6, “Best” : 15}]
Explanation : All elements are extracted which have “Best” other than 10.
方法#1:使用列表推导
这是可以执行此任务的方式之一。在此,我们使用条件检查在一个衬里中使用列表理解进行提取和迭代。
Python3
# Python3 code to demonstrate working of
# Remove Dictionaries whose Key(K) is N
# Using list comprehension
# initializing list
test_list = [{"Gfg" : 3, "is" : 7, "Best" : 8},
{"Gfg" : 9, "is" : 2, "Best" : 9},
{"Gfg" : 5, "is" : 4, "Best" : 10},
{"Gfg" : 3, "is" : 6, "Best" : 8}]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = "Gfg"
# initializing N
N = 5
# returning only dictionaries with "Gfg" key not 5
res = [sub for sub in test_list if sub[K] != N]
# printing result
print("The extracted dictionaries : " + str(res))
Python3
# Python3 code to demonstrate working of
# Remove Dictionaries whose Key(K) is N
# Using filter() + lambda
# initializing list
test_list = [{"Gfg" : 3, "is" : 7, "Best" : 8},
{"Gfg" : 9, "is" : 2, "Best" : 9},
{"Gfg" : 5, "is" : 4, "Best" : 10},
{"Gfg" : 3, "is" : 6, "Best" : 8}]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = "Gfg"
# initializing N
N = 5
# Using filter() to check for N value
res = list(filter(lambda x: x[K] != N, test_list))
# printing result
print("The extracted dictionaries : " + str(res))
The original list : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 5, 'is': 4, 'Best': 10}, {'Gfg': 3, 'is': 6, 'Best': 8}]
The extracted dictionaries : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 3, 'is': 6, 'Best': 8}]
方法 #2:使用 filter() + lambda
这是可以执行此任务的另一种方式。在此,我们使用 filter() 和 lambda函数用于检查 N 值的条件。
Python3
# Python3 code to demonstrate working of
# Remove Dictionaries whose Key(K) is N
# Using filter() + lambda
# initializing list
test_list = [{"Gfg" : 3, "is" : 7, "Best" : 8},
{"Gfg" : 9, "is" : 2, "Best" : 9},
{"Gfg" : 5, "is" : 4, "Best" : 10},
{"Gfg" : 3, "is" : 6, "Best" : 8}]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = "Gfg"
# initializing N
N = 5
# Using filter() to check for N value
res = list(filter(lambda x: x[K] != N, test_list))
# printing result
print("The extracted dictionaries : " + str(res))
The original list : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 5, 'is': 4, 'Best': 10}, {'Gfg': 3, 'is': 6, 'Best': 8}]
The extracted dictionaries : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 3, 'is': 6, 'Best': 8}]