Python – 字典列表中的检查列表元素
有时,在处理数据时,我们可能会遇到需要检查列表元素是否存在作为记录列表中的特定键的问题。此类问题可能发生在涉及数据的领域,例如 Web 开发和机器学习。让我们讨论可以解决此任务的某些方法。
Input : test_list = [{‘Price’: 20, ‘Color’: ‘Orange’}, {‘Price’: 25, ‘Color’: ‘Yellow’}]
Output : [True, False, True, False]
Input : test_list = [{‘Color’: ‘Pink’, ‘Price’: 50}]
Output : [False, False, False, False]
方法#1:使用循环
这是解决这个问题的粗暴方法。在此,我们将迭代列表中每个值的所有字典,并与所需的键进行比较,并为拥有它的记录返回 True。
# Python3 code to demonstrate working of
# Check List elements from Dictionary List
# Using loop
# helpr_func
def check_ele(ele, test_list):
for sub in test_list:
for item in sub.values():
if ele == item:
return True
return False
# initializing list
test_list = [{'Name' : 'Apple', 'Price' : 18, 'Color' : 'Red'},
{'Name' : 'Mango', 'Price' : 20, 'Color' : 'Yellow'},
{'Name' : 'Orange', 'Price' : 24, 'Color' : 'Orange'},
{'Name' : 'Plum', 'Price' : 28, 'Color' : 'Red'}]
# printing original list
print("The original list is : " + str(test_list))
# initializing Values list
val_list = ['Yellow', 'Red', 'Orange', 'Green']
# Check List elements from Dictionary List
# Using loop
res = []
for ele in val_list:
res.append(check_ele(ele, test_list))
# printing result
print("The Association list in Order : " + str(res))
The original list is : [{‘Name’: ‘Apple’, ‘Color’: ‘Red’, ‘Price’: 18}, {‘Name’: ‘Mango’, ‘Color’: ‘Yellow’, ‘Price’: 20}, {‘Name’: ‘Orange’, ‘Color’: ‘Orange’, ‘Price’: 24}, {‘Name’: ‘Plum’, ‘Color’: ‘Red’, ‘Price’: 28}]
The Association list in Order : [True, True, True, False]
方法 #2:使用any()
+ 生成器表达式
使用 any() 与生成器表达式集成可以解决这个问题。在此,我们通过减少内部循环,通过使用 any() 进行测试来减少代码行数。
# Python3 code to demonstrate working of
# Check List elements from Dictionary List
# Using any() + generator expression
# initializing list
test_list = [{'Name' : 'Apple', 'Price' : 18, 'Color' : 'Red'},
{'Name' : 'Mango', 'Price' : 20, 'Color' : 'Yellow'},
{'Name' : 'Orange', 'Price' : 24, 'Color' : 'Orange'},
{'Name' : 'Plum', 'Price' : 28, 'Color' : 'Red'}]
# printing original list
print("The original list is : " + str(test_list))
# initializing Values list
val_list = ['Yellow', 'Red', 'Orange', 'Green']
# initializing Key
key = 'Color'
# Check List elements from Dictionary List
# Using loop
res = [any(clr == sub[key] for sub in test_list) for clr in val_list]
# printing result
print("The Association list in Order : " + str(res))
The original list is : [{‘Name’: ‘Apple’, ‘Color’: ‘Red’, ‘Price’: 18}, {‘Name’: ‘Mango’, ‘Color’: ‘Yellow’, ‘Price’: 20}, {‘Name’: ‘Orange’, ‘Color’: ‘Orange’, ‘Price’: 24}, {‘Name’: ‘Plum’, ‘Color’: ‘Red’, ‘Price’: 28}]
The Association list in Order : [True, True, True, False]