在Python中检查一个值是否存在于二维列表中
Check if value exists in a Two-dimensional list in Python
使用该any()
函数检查一个值是否存在于二维列表中。
如果该值存在于列表中,则
该any()
函数将返回,否则返回。True
False
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any('three' in nested_list for nested_list in my_2d_list): # 👇️ this runs print('three is contained in the two-dimensional list') else: print('three is NOT contained in the two-dimensional list')
我们使用生成器表达式迭代二维列表。
在每次迭代中,我们检查嵌套列表中是否包含特定值。
any函数接受一个可迭代对象作为参数,如果可迭代对象中的任何元素为真则返回。True
any()
True
定义一个可重用的函数
如果您必须经常这样做,请定义一个可重用的函数。
def is_in_2d_list(two_dimensional_list, value): return any( value in nested_list for nested_list in two_dimensional_list ) my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] print(is_in_2d_list(my_2d_list, 'one')) # 👉️ True print(is_in_2d_list(my_2d_list, 'abc')) # 👉️ False if is_in_2d_list(my_2d_list, 'one'): print('The value is in the two-dimensional list') else: print('The value is NOT in the two-dimensional list')
该函数以一个二维列表和一个值作为参数,
True
如果该值包含在二维列表中则返回,false
否则返回。
获取包含值的嵌套列表
如果需要获取包含指定值的嵌套列表,可以使用
赋值表达式语法。
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any('three' in (match := nested_list) for nested_list in my_2d_list): # 👇️ this runs print('three is contained in the two-dimensional list') print(match) # 👉️ ['three', 'four'] print(match.index('three')) # 👉️ 0 else: print('three is NOT contained in the two-dimensional list')
Assignment expressions allow us to assign to variables within an expression
using the NAME := expression
syntax.
If the iterable is empty or none of the elements in the iterable are truthy, the
any
function returns False
.
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any('HELLO' in nested_list for nested_list in my_2d_list): print('HELLO is contained in the two-dimensional list') else: # 👇️ this runs print('HELLO is NOT contained in the two-dimensional list')
None of the nested lists contain an item with the specified value, so the
condition is never met and the any()
function returns False
.
Alternatively, you can use a for loop.
# Check if a Value exists in a Two-dimensional List using a for
loop
This is a three-step process:
- Use a
for
loop to iterate over the list of lists. - Use the
in
operator to check if each sublist contains the value. - Break out of the loop once you find a list that contains the value.
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] exists_in_list = False for nested_list in my_2d_list: if 'three' in nested_list: exists_in_list = True print(nested_list) break if exists_in_list: print('The value exists in the two-dimensional list') else: print('The value does NOT exist in the two-dimensional list')
We used a for
loop to iterate over the two-dimensional list.
On each iteration, we check if the current sublist contains a given value.
If the value is contained in the sublist, we set the exists_in_list
variable
to True
and break out of the loop.
The break statement breaks out of the
innermost enclosing for
or while
loop.
If the value is not contained in the two-dimensional list, the exists_in_list
variable remains set to False
.
# Additional Resources
You can learn more about the related topics by checking out the following
tutorials: