检查 List 中的所有/任何元素是否满足 Python 中的条件

目录

Check if any element in list meets a condition in Python

  1. 检查列表中的任何元素是否满足 Python 中的条件
  2. 检查列表中的所有元素是否满足 Python 中的条件

检查列表中的任何元素是否满足 Python 中的条件

使用该any()函数检查列表中的任何元素是否满足条件。

如果列表中的任何元素满足条件,any函数将返回,否则返回。TrueFalse

主程序
my_list = [1, 3, 5, 15] if any(item > 10 for item in my_list): # 👇️ this runs print('There is an item greater than 10') else: print('No items in the list are greater than 10') # 👇️ True print(any(item > 10 for item in my_list)) # 👇️ False print(any(item > 50 for item in my_list))

如果您需要检查列表中的所有元素是否满足条件,请单击以下子标题:

any函数接受一个可迭代对象作为参数,如果可迭代对象中的任何元素为真则返回。True

我们将生成器表达式传递给函数any()

生成器表达式用于对每个元素执行某些操作或选择满足条件的元素子集。

在示例中,我们检查列表中的每一项是否大于10并返回结果。

获取满足条件的元素

如果需要获取满足条件的元素,可以使用赋值表达式语法。

主程序
my_list = [1, 3, 5, 15] if any((match := item) > 10 for item in my_list): # 👇️ this runs print('There is an item greater than 10') print(match) # 👉️ 15 else: print('No items in the list are greater than 10')

赋值表达式允许我们使用语法给表达式中的变量赋值NAME := expression

If the condition is met at least once, the any() function returns True.

If the iterable we pass to the any()function is empty or none of the elements in the iterable are truthy, the any function returns False.
main.py
my_list = [1, 3, 5] if any(item > 10 for item in my_list): print('There is an item greater than 10') else: # 👇️ this runs print('No items in the list are greater than 10')

None of the items in the list is greater than 10, so the condition is never
met and any() returns False.

If your list is empty, the any function will always return False.

main.py
my_list = [] if any(item > 10 for item in my_list): print('There is an item greater than 10') else: # 👇️ this runs print('No items in the list are greater than 10')

Here is another example that uses the any() function to check if at least 1
item in the list has a None value.

main.py
my_list = ['a', 'b', None, 'd'] if any(item is None for item in my_list): # 👇️ This runs print('There is a None value in the list') else: print('No items in the list have a value of None') # 👇️ True print(any(item is None for item in my_list))

The generator expression we passed to the any() function iterates over the
list and checks if each value is None.

If the condition is met at least once, the any function returns True,
otherwise, it returns False.

# Check if ANY element in a list meets a condition using a for loop

You can also use a for loop to check if any element in a list meets a
condition.

main.py
my_list = [1, 3, -4, 5, 15, -3] any_meets_condition = False for element in my_list: if element > 10: any_meets_condition = True print(element) # 👉️ 15 break print(any_meets_condition) # 👉️ True

We used a for loop to iterate over the list

On each iteration, we check if the current element is greater than 10.

If the condition is met, we set the any_meets_condition variable to True and
exit the loop.

You also have access to the element that meets the condition in the if block.

# Check if ALL elements in a List meet a condition in Python

Use the all() function to check if all elements in a list meet a
condition.

The all function will return True if all elements in the list meet the
condition and False otherwise.

main.py
my_list = [1, 3, 5, 15] if all(item > 0 for item in my_list): # 👇️ this runs print('All elements in the list are greater than 0') else: print('Not all elements in the list are greater than 0') # 👇️ True print(all(item > 0 for item in my_list)) # 👇️ False print(all(item > 10 for item in my_list))

The all() built-in function
takes an iterable as an argument and returns True if all elements in the
iterable are truthy (or the iterable is empty).

We passed a generator expression to the all() function.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

In the example, we check if all elements in the list are greater than 0.

The all function will return True if all elements in the list meet the
condition and False otherwise.

If a single value that doesn’t meet the condition is encountered, the all()
function short-circuits returning False.

# Check if ALL elements in a List meet a condition using a for loop

You can also use a basic for loop to check if
all elements in a list meet a condition.

main.py
my_list = [1, 3, 5, 15] all_meet_condition = True for element in my_list: if element < 0: all_meet_condition = False break print(all_meet_condition) # 👉️ True

We initialized a variable to True and used a for loop to iterate over the
list.

On each iteration, we check if the current item is less than 0.

If the condition is met, we set the all_meet_condition variable to False and
break out of the loop.

If the condition is never met, the all_meet_condition variable remains set to
True.

The break statement breaks out of the
innermost enclosing for or while loop.

You can also use this approach to get the elements that don’t meet the condition
or to get the ones that do.

main.py
my_list = [1, 3, -4, 5, 15, -3] all_meet_condition = True meet_condition = [] dont_meet_condition = [] for element in my_list: if element < 0: all_meet_condition = False dont_meet_condition.append(element) else: meet_condition.append(element) print(all_meet_condition) # 👉️ False print(meet_condition) # 👉️ [1, 3, 5, 15] print(dont_meet_condition) # 👉️ [-4, -3]

We used a for loop to iterate over the list.

在每次迭代中,我们检查当前元素是否小于0

如果满足条件,我们将值附加到列表中dont_meet_condition

否则,该值将附加到meet_condition列表中。

额外资源

您可以通过查看以下教程来了解有关相关主题的更多信息: