如何检查数字是否在 Python 列表中

PythonBeginner
立即练习

介绍

在这个实验中,你将学习如何在 Python 中检查一个数字或任何项是否存在于列表中。这是列表操作和基于元素存在性进行决策的基本操作。

本实验将指导你使用 in 运算符来测试列表成员资格,并展示其区分大小写的特性。你将创建一个水果列表,并使用 in 运算符来检查特定水果是否存在,包括存在的、不存在的以及仅大小写不同的水果。你还将学习如何使用 index() 方法来定位列表中元素的位置。

理解列表成员资格

在这一步中,你将学习如何在 Python 中检查一个项是否存在于列表中。这是处理列表时的基本操作,常用于根据特定元素的存在与否来做出决策。

Python 提供了一个方便的运算符 in 来测试成员资格。如果在列表中找到该项,in 运算符将返回 True,否则返回 False

让我们从创建一个水果列表开始:

fruits = ["apple", "banana", "orange", "grape"]

现在,让我们使用 in 运算符来检查 "apple" 是否在 fruits 列表中。使用 VS Code 编辑器在你的 ~/project 目录下创建一个名为 membership.py 的文件:

## ~/project/membership.py
fruits = ["apple", "banana", "orange", "grape"]
print("apple" in fruits)

保存文件并在终端中使用以下命令运行它:

python ~/project/membership.py

你应该会看到以下输出:

True

这表明 "apple" 确实是 fruits 列表的成员。

现在,让我们检查一个不在列表中的项,比如 "kiwi"

修改你的 membership.py 文件以检查 "kiwi"

## ~/project/membership.py
fruits = ["apple", "banana", "orange", "grape"]
print("kiwi" in fruits)

保存文件并再次运行它:

python ~/project/membership.py

你应该会看到以下输出:

False

这证实了 "kiwi" 不是 fruits 列表的成员。

in 运算符区分大小写。这意味着 "Apple""apple" 不同。让我们来演示一下:

修改你的 membership.py 文件:

## ~/project/membership.py
fruits = ["apple", "banana", "orange", "grape"]
print("Apple" in fruits)

保存文件并运行它:

python ~/project/membership.py

你应该会看到:

False

尽管 "apple" 在列表中,但 "Apple"(首字母大写)不在。

你还可以使用 not in 运算符来检查一个项是否 不在 列表中。例如:

修改你的 membership.py 文件:

## ~/project/membership.py
fruits = ["apple", "banana", "orange", "grape"]
print("kiwi" not in fruits)

保存文件并运行它:

python ~/project/membership.py

输出将是:

True

这是因为 "kiwi" 不在 fruits 列表中。

理解列表成员资格对于编写条件语句和控制 Python 程序的流程至关重要。

使用 in 运算符

在上一步中,你学习了列表成员资格的基础知识,以及如何使用 in 运算符检查一个项是否存在于列表中。在这一步,我们将探索 in 运算符更多的实际应用,包括在条件语句中使用它来控制程序的流程。

in 运算符在 if 语句中特别有用。你可以使用它来仅当特定项存在(或不存在)于列表中时执行特定的代码块。

让我们继续使用我们的 fruits 列表示例。假设你只想在 "banana" 在列表中时打印一条消息。在你的 ~/project 目录下创建一个名为 in_operator.py 的文件:

## ~/project/in_operator.py
fruits = ["apple", "banana", "orange", "grape"]

if "banana" in fruits:
    print("Yes, banana is in the list")

保存文件并运行它:

python ~/project/in_operator.py

你应该会看到以下输出:

Yes, banana is in the list

if 语句检查 "banana" 是否在 fruits 列表中。如果是,则执行 if 块内的代码(print 语句)。

现在,让我们添加一个 else 块来处理项不在列表中的情况:

修改你的 in_operator.py 文件:

## ~/project/in_operator.py
fruits = ["apple", "banana", "orange", "grape"]

if "kiwi" in fruits:
    print("Yes, kiwi is in the list")
else:
    print("No, kiwi is not in the list")

保存文件并运行它:

python ~/project/in_operator.py

你应该会看到以下输出:

No, kiwi is not in the list

由于 "kiwi" 不在 fruits 列表中,因此执行 else 块内的代码。

你还可以在 if 语句中使用 not in 运算符。例如:

修改你的 in_operator.py 文件:

## ~/project/in_operator.py
fruits = ["apple", "banana", "orange", "grape"]

if "kiwi" not in fruits:
    print("kiwi is not in the list")

保存文件并运行它:

python ~/project/in_operator.py

输出将是:

kiwi is not in the list

这展示了你如何在 Python 程序中使用 innot in 运算符来创建更复杂的逻辑。这些运算符是处理列表并根据其内容做出决策的重要工具。

使用 index() 方法定位位置

在前面的步骤中,你学习了如何使用 in 运算符检查一个项是否存在于列表中。现在,你将学习如何使用 index() 方法来查找列表中某个项的位置(索引)。

index() 方法返回指定值在列表中首次出现的索引。需要注意的是,如果在列表中未找到该项,index() 方法将引发 ValueError 异常。

让我们继续使用 fruits 列表示例。假设你想找到 "banana" 在列表中的索引。在你的 ~/project 目录下创建一个名为 index_method.py 的文件:

## ~/project/index_method.py
fruits = ["apple", "banana", "orange", "grape"]

index = fruits.index("banana")
print(index)

保存文件并运行它:

python ~/project/index_method.py

你应该会看到以下输出:

1

这表明 "banana"fruits 列表中的索引为 1。请记住,Python 列表的索引是从 0 开始的,这意味着第一个元素的索引为 0,第二个元素的索引为 1,依此类推。

现在,让我们尝试查找一个不在列表中的项的索引,比如 "kiwi"

修改你的 index_method.py 文件:

## ~/project/index_method.py
fruits = ["apple", "banana", "orange", "grape"]

index = fruits.index("kiwi")
print(index)

保存文件并运行它:

python ~/project/index_method.py

你应该会看到类似以下的错误消息:

Traceback (most recent call last):
  File "/home/labex/project/index_method.py", line 3, in <module>
    index = fruits.index("kiwi")
ValueError: 'kiwi' is not in list

这个 ValueError 表明 "kiwi" 未在列表中找到。

为避免此错误,在使用 index() 方法之前,先使用 in 运算符检查该项是否存在于列表中是个不错的做法。

以下是具体实现方法:

修改你的 index_method.py 文件:

## ~/project/index_method.py
fruits = ["apple", "banana", "orange", "grape"]

if "kiwi" in fruits:
    index = fruits.index("kiwi")
    print(index)
else:
    print("kiwi is not in the list")

保存文件并运行它:

python ~/project/index_method.py

你应该会看到以下输出:

kiwi is not in the list

这段代码首先检查 "kiwi" 是否在 fruits 列表中。如果存在,它会找到其索引并打印出来;否则,它会打印一条消息表明 "kiwi" 不在列表中,从而避免了 ValueError 异常。

index() 方法是查找列表中项的位置的有用工具,但为避免错误,处理项可能不存在的情况很重要。

总结

在这个实验中,你学习了如何使用 in 运算符来检查一个项是否存在于 Python 列表中。如果在列表中找到该项,in 运算符将返回 True,否则返回 False

你还了解到 in 运算符是区分大小写的,这意味着 "Apple""apple" 是不同的。本实验通过一个水果列表的示例,展示了如何检查特定项是否存在,以此说明了这些概念。