Python 3中列表的理解

介绍

A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].

列表在处理许多相关数值时非常有用。它们使您能够将属于同一组的数据放在一起,简化您的代码,并一次对多个值执行相同的方法和操作。

在考虑Python列表和其他类型的集合数据结构时,考虑一下您计算机上拥有的所有不同集合:您的文件组合,歌曲播放列表,浏览器书签,电子邮件,您可以在流媒体服务上访问的视频集合等。

先决条件

您应该已经安装了Python 3,并在您的计算机或服务器上设置了编程环境。如果您尚未设置编程环境,可以参考适用于您操作系统(Ubuntu、CentOS、Debian等)的 本地编程环境 或 服务器上的编程环境 的安装和设置指南。

字符串列表

要开始,让我们创建一个包含字符串数据类型项目的列表:

信息:要跟着本教程中的示例代码,打开本地系统上的Python交互式shell,运行python3命令。然后您可以通过在>>>提示后添加、复制或编辑示例来进行操作。

sea_creatures = ['shark', 'cuttlefish', 'squid', 'mantis shrimp', 'anemone']

当我们打印列表时,输出的响应与我们创建的列表完全相同:

print(sea_creatures)
Output
['shark', 'cuttlefish', 'squid', 'mantis shrimp', 'anemone']

作为一个有序序列的元素,列表中的每个项目都可以通过索引单独调用。列表是由较小的部分组成的复合数据类型,非常灵活,因为它们可以添加、删除和更改值。当您需要存储大量值或迭代值,并且希望能够方便地修改这些值时,您可能希望使用列表数据类型。

在本教程中,我们将介绍一些我们可以在Python中使用列表的方法。

索引列表

列表中的每个项目对应一个索引号,这是一个整数值,从索引号0开始。

对于列表sea_creatures,索引的分布如下:

‘shark’ ‘cuttlefish’ ‘squid’ ‘mantis shrimp’ ‘anemone’
0 1 2 3 4

第一项,字符串'shark'从索引0开始,列表在索引4处以项'anemone'结束。

因为Python列表中的每个项都有相应的索引号,所以我们能够以与其他顺序数据类型相同的方式访问和操作列表。

现在我们可以通过引用其索引号来调用列表的离散项:

print(sea_creatures[1])
Output
cuttlefish

该列表的索引号范围从04,如上表所示。因此,要单独调用任何项,我们会像这样引用索引号:

sea_creatures[0] = 'shark'
sea_creatures[1] = 'cuttlefish'
sea_creatures[2] = 'squid'
sea_creatures[3] = 'mantis shrimp'
sea_creatures[4] = 'anemone'

如果我们使用大于4的索引号调用列表sea_creatures,它将超出范围,因为它将无效:

print(sea_creatures[18])
Output
IndexError: list index out of range

除了正索引号,我们还可以通过使用负索引号从列表中访问项,从列表末尾开始向后计数,从-1开始。如果我们有一个很长的列表,并且想要准确地定位列表末尾的项,则这是特别有用的。

对于同一个列表sea_creatures,负索引的分解如下:

‘shark’ ‘cuttlefish’ ‘squid’ ‘mantis shrimp’ ‘anemone’
-5 -4 -3 -2 -1

因此,如果我们想要通过使用其负索引号打印出项'squid',我们可以像这样做:

print(sea_creatures[-3])
Output
squid

我们可以使用+运算符将列表中的字符串项与其他字符串连接起来:

print('Sammy is a ' + sea_creatures[0])
Output
Sammy is a shark

我们能够将索引号为0的字符串项目与字符串'Sammy is a '连接起来。我们还可以使用+运算符来连接两个或多个列表

通过与列表中的项目对应的索引号,我们能够离散地访问列表的每个项目并处理这些项目。

修改列表中的项目

我们可以使用索引来更改列表中的项目,将索引号设置为不同的值。这使我们能够更好地控制列表,因为我们能够修改和更新它们包含的项目。

如果我们想要将索引1处项目的字符串值从'cuttlefish'更改为'octopus',我们可以这样做:

sea_creatures[1] = 'octopus'

现在当我们打印sea_creatures时,列表将会不同:

print(sea_creatures)
Output
['shark', 'octopus', 'squid', 'mantis shrimp', 'anemone']

我们还可以使用负索引号来更改项目的值:

sea_creatures[-3] = 'blobfish'
print(sea_creatures)
Output
['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone']

现在'blobfish'已经取代了'squid',在负索引号-3处(对应于正索引号2)。

能够修改列表中的项目使我们能够以高效的方式更改和更新列表。

切片列表

我们还可以从列表中调用几个项目。假设我们只想打印sea_creatures的中间项目,我们可以通过创建切片来实现。使用切片,我们可以通过创建由冒号分隔的索引号范围来调用多个值:[x:y]

print(sea_creatures[1:4])
Output
['octopus', 'blobfish', 'mantis shrimp']

在创建切片时,就像[1:4]一样,第一个索引号是切片开始的位置(包括),第二个索引号是切片结束的位置(不包括),这就是为什么在我们上面的示例中位置为123的项目被打印出来。

如果我们想要包括列表的任一端,我们可以在list[x:y]语法中省略一个数字。例如,如果我们想要打印列表sea_creatures的前3个项目 —— 分别是'shark''octopus''blobfish',我们可以这样做:

print(sea_creatures[:3])
Output
['shark', 'octopus', 'blobfish']

这将打印列表的开头,停在索引3之前。

要包括列表末尾的所有项目,我们需要颠倒语法:

print(sea_creatures[2:])
Output
['blobfish', 'mantis shrimp', 'anemone']

我们还可以在切片列表时使用负索引号,类似于正索引号:

print(sea_creatures[-4:-2])
print(sea_creatures[-3:])
Output
['octopus', 'blobfish'] ['blobfish', 'mantis shrimp', 'anemone']

我们可以在切片中使用的最后一个参数叫做步幅(stride),它指的是在从列表中检索第一项后向前移动多少项。到目前为止,我们已经省略了步幅参数,默认为 Python 的步幅为 1,这样就可以在两个索引号之间检索到每一项。

这个结构的语法是list[x:y:z],其中z表示步幅。让我们创建一个更大的列表,然后对其进行切片,并将步幅设置为 2:

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

print(numbers[1:11:2])
Output
[1, 3, 5, 7, 9]

我们的结构numbers[1:11:2]打印出了索引号为111之间的值,然后步幅值为2告诉程序只打印出每隔一项。

我们可以省略前两个参数,仅使用步幅作为参数,语法为list[::z]

print(numbers[::3])
Output
[0, 3, 6, 9, 12]

通过将列表numbers的步幅设置为3,只打印出每三个项:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12

使用正索引号和负索引号对列表进行切片,并指定步幅,使我们能够控制列表并获得我们想要的输出。

使用运算符修改列表

运算符可以用于对列表进行修改。我们将回顾使用+*运算符及其复合形式+=*=

+运算符可用于将两个或多个列表连接在一起:

sea_creatures = ['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone']
oceans = ['Pacific', 'Atlantic', 'Indian', 'Southern', 'Arctic']

print(sea_creatures + oceans)
Output
['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'Pacific', 'Atlantic', 'Indian', 'Southern', 'Arctic']

由于+运算符可以连接,因此可以用于将列表形式的项目(或多个项目)添加到另一个列表的末尾。记得将项目放在方括号中:

sea_creatures = sea_creatures + ['yeti crab']
print (sea_creatures)
Output
['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab']

*运算符可用于对列表进行乘法运算。也许您需要将目录中的所有文件复制到服务器上,或者与朋友分享播放列表——在这些情况下,您需要复制数据集。

让我们将sea_creatures列表乘以2,将oceans列表乘以3:

print(sea_creatures * 2)
print(oceans * 3)
Output
['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab', 'shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab'] ['Pacific', 'Atlantic', 'Indian', 'Southern', 'Arctic', 'Pacific', 'Atlantic', 'Indian', 'Southern', 'Arctic', 'Pacific', 'Atlantic', 'Indian', 'Southern', 'Arctic']

通过使用*运算符,我们可以按照我们指定的次数复制我们的列表。

我们还可以使用+*运算符的复合形式与赋值运算符=一起使用。+=*=复合运算符可用于以快速和自动化的方式填充列表。您可以使用这些运算符填充具有占位符的列表,稍后可以使用用户提供的输入进行修改,例如。

让我们将一个列表形式的项目添加到列表sea_creatures中。这个项目将作为占位符,我们希望多次添加这个占位符项目。为此,我们将使用+=运算符与for循环

for x in range(1,4):
    sea_creatures += ['fish']
    print(sea_creatures)
Output
['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab', 'fish'] ['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab', 'fish', 'fish'] ['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab', 'fish', 'fish', 'fish']

对于for循环的每次迭代,都会向原始列表sea_creatures添加一个额外的列表项'fish'

*=运算符的行为类似:

sharks = ['shark']

for x in range(1,4):
    sharks *= 2
    print(sharks)
Output
['shark', 'shark'] ['shark', 'shark', 'shark', 'shark'] ['shark', 'shark', 'shark', 'shark', 'shark', 'shark', 'shark', 'shark']

运算符+*可用于连接列表和复制列表。复合运算符+=*=可用于连接列表和复制列表,并将新的标识传递给原始列表。

从列表中删除项目

可以使用del语句从列表中删除项目。这将删除列表中指定索引号处的值。

sea_creatures列表中,让我们删除项目'octopus'。该项目位于索引位置1。要删除该项,我们将使用del语句,然后调用列表变量和该项的索引号:

sea_creatures =['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab']

del sea_creatures[1]
print(sea_creatures)
Output
['shark', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab']

现在位于索引位置1的项目,字符串'octopus',不再存在于我们的列表sea_creatures中。

我们也可以使用del语句指定一个范围。假设我们不仅想要删除项目'octopus',还要删除'blobfish''mantis shrimp'。我们可以使用del语句调用sea_creatures中的一个范围来实现这一点:

sea_creatures =['shark', 'octopus', 'blobfish', 'mantis shrimp', 'anemone', 'yeti crab']

del sea_creatures[1:4]
print(sea_creatures)
Output
['shark', 'anemone', 'yeti crab']

通过使用del语句的范围,我们能够删除索引号为1(包含)和索引号为4(不包含)之间的项目,从而使我们得到一个删除了3个项目后剩余3个项目的列表。

del语句允许我们从列表数据类型中删除特定项目。

使用列表项构建列表

列表可以由包含列表的项目定义,每个括号内的列表都包含在父列表的较大括号内:

sea_names = [['shark', 'octopus', 'squid', 'mantis shrimp'],['Sammy', 'Jesse', 'Drew', 'Jamie']]

这些列表中的嵌套列表称为嵌套列表。

要访问此列表中的项目,我们将不得不使用多个索引:

print(sea_names[1][0])
print(sea_names[0][0])
Output
Sammy shark

第一个列表,因为它等于一个项目,将具有索引号0,这将是构造中的第一个数字,而第二个列表将具有索引号1。在每个内部嵌套列表中,将有单独的索引号,我们将在第二个索引号中调用:

sea_names[0][0] = 'shark'
sea_names[0][1] = 'octopus'
sea_names[0][2] = 'squid'
sea_names[0][3] = 'mantis shrimp'

sea_names[1][0] = 'Sammy'
sea_names[1][1] = 'Jesse'
sea_names[1][2] = 'Drew'
sea_names[1][3] = 'Jamie'

在处理列表的列表时,重要的是要记住,您需要引用多个索引号才能访问相关嵌套列表中的特定项目。

结论

列表数据类型是一种灵活的数据类型,在程序执行过程中可以进行修改。本教程涵盖了列表的基本特性,包括索引、切片、修改和连接列表。

从这里开始,您可以通过阅读“如何使用列表方法”来了解更多关于在Python中使用列表的内容,并且了解列表推导来根据现有列表创建列表。要了解更多关于数据类型的内容,您可以阅读我们的“理解数据类型”教程。

Source:
https://www.digitalocean.com/community/tutorials/understanding-lists-in-python-3