Python - 按列表中第 K 个键中的值过滤字典
给定一个字典列表,任务是编写一个Python程序,根据列表中第 K 个键的元素来过滤字典。
例子:
Input : test_list = [{“Gfg” : 3, “is” : 5, “best” : 10},
{“Gfg” : 5, “is” : 1, “best” : 1},
{“Gfg” : 8, “is” : 3, “best” : 9},
{“Gfg” : 9, “is” : 9, “best” : 8},
{“Gfg” : 4, “is” : 10, “best” : 7}], K = “best”, search_list = [1, 9, 8, 4, 5]
Output : [{‘Gfg’: 5, ‘is’: 1, ‘best’: 1}, {‘Gfg’: 8, ‘is’: 3, ‘best’: 9}, {‘Gfg’: 9, ‘is’: 9, ‘best’: 8}]
Explanation : Dictionaries with “best” as key and values other than 1, 9, 8, 4, 5 are omitted.
Input : test_list = [{“Gfg” : 3, “is” : 5, “best” : 10},
{“Gfg” : 5, “is” : 1, “best” : 1},
{“Gfg” : 8, “is” : 3, “best” : 9}], K = “best”, search_list = [1, 9, 4, 5]
Output : [{‘Gfg’: 5, ‘is’: 1, ‘best’: 1}, {‘Gfg’: 8, ‘is’: 3, ‘best’: 9}]
Explanation : Dictionaries with “best” as key and values other than 1, 9, 4, 5 are omitted.
方法#1:使用循环+条件语句
在这种情况下,使用条件检查列表中的第 K 个键值后,键值对被添加到结果字典中,使用循环进行迭代。
Python3
# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using loop + conditional statements
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
{"Gfg": 5, "is": 1, "best": 1},
{"Gfg": 8, "is": 3, "best": 9},
{"Gfg": 9, "is": 9, "best": 8},
{"Gfg": 4, "is": 10, "best": 7}]
# printing original list
print("The original list is : " + str(test_list))
# initializing search_list
search_list = [1, 9, 8, 4, 5]
# initializing K
K = "best"
res = []
for sub in test_list:
# checking if Kth key's value present in search_list
if sub[K] in search_list:
res.append(sub)
# printing result
print("Filtered dictionaries : " + str(res))
Python3
# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using list comprehension
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
{"Gfg": 5, "is": 1, "best": 1},
{"Gfg": 8, "is": 3, "best": 9},
{"Gfg": 9, "is": 9, "best": 8},
{"Gfg": 4, "is": 10, "best": 7}, ]
# printing original list
print("The original list is : " + str(test_list))
# initializing search_list
search_list = [1, 9, 8, 4, 5]
# initializing K
K = "best"
# list comprehension as shorthand for solving problem
res = [sub for sub in test_list if sub[K] in search_list]
# printing result
print("Filtered dictionaries : " + str(res))
输出:
The original list is : [{‘Gfg’: 3, ‘is’: 5, ‘best’: 10}, {‘Gfg’: 5, ‘is’: 1, ‘best’: 1}, {‘Gfg’: 8, ‘is’: 3, ‘best’: 9}, {‘Gfg’: 9, ‘is’: 9, ‘best’: 8}, {‘Gfg’: 4, ‘is’: 10, ‘best’: 7}]
Filtered dictionaries : [{‘Gfg’: 5, ‘is’: 1, ‘best’: 1}, {‘Gfg’: 8, ‘is’: 3, ‘best’: 9}, {‘Gfg’: 9, ‘is’: 9, ‘best’: 8}]
方法#2:使用列表理解
与上述方法类似,列表理解用于为上述方法提供速记。
蟒蛇3
# Python3 code to demonstrate working of
# Filter dictionaries by values in Kth Key in list
# Using list comprehension
# initializing list
test_list = [{"Gfg": 3, "is": 5, "best": 10},
{"Gfg": 5, "is": 1, "best": 1},
{"Gfg": 8, "is": 3, "best": 9},
{"Gfg": 9, "is": 9, "best": 8},
{"Gfg": 4, "is": 10, "best": 7}, ]
# printing original list
print("The original list is : " + str(test_list))
# initializing search_list
search_list = [1, 9, 8, 4, 5]
# initializing K
K = "best"
# list comprehension as shorthand for solving problem
res = [sub for sub in test_list if sub[K] in search_list]
# printing result
print("Filtered dictionaries : " + str(res))
输出:
The original list is : [{‘Gfg’: 3, ‘is’: 5, ‘best’: 10}, {‘Gfg’: 5, ‘is’: 1, ‘best’: 1}, {‘Gfg’: 8, ‘is’: 3, ‘best’: 9}, {‘Gfg’: 9, ‘is’: 9, ‘best’: 8}, {‘Gfg’: 4, ‘is’: 10, ‘best’: 7}]
Filtered dictionaries : [{‘Gfg’: 5, ‘is’: 1, ‘best’: 1}, {‘Gfg’: 8, ‘is’: 3, ‘best’: 9}, {‘Gfg’: 9, ‘is’: 9, ‘best’: 8}]