Python 入门:列表(Lists)

更新于 2026-01-13

Jack Wallen 2021-12-31

在 Python 中,列表(list) 用于将多个项目存储在一个变量中。你还记得变量吧:

color = "blue"

在上面的例子中,我们将值 "blue" 赋给了变量 color。然后我们可以在脚本中这样使用该变量:

print(color)

当我们运行程序时,它会输出:

blue

很简单。但如果我们想要定义多种颜色呢?当然,我们也可以这样做:

color1 = "blue"
color2 = "green"
color3 = "red"
color4 = "yellow"

要打印出这些颜色,我们可以这样做:

print(color1, color2, color3, color4)

我们的脚本看起来会是这样:

# 将我们的变量定义为颜色
color1 = "blue"
color2 = "green"
color3 = "red"
color4 = "yellow"

# 打印出颜色列表
print(color1, color2, color3, color4)

你可能不会感到惊讶——其实借助列表(list),我们可以更轻松地完成这件事。让我们继续以颜色为例。现在,我们将 color 变量定义为一个包含四种颜色的列表。这个变量声明如下所示:

color = ['blue', 'green', 'red', 'yellow']

现在,有一件事情你必须理解。你可能会认为列表中每个项目的序号是:blue = 1,green = 2,red = 3,yellow = 4。但重要的是要明白,在软件开发领域,计数是从 0 开始的。因此,我们颜色的实际位置应该是:blue = 0,green = 1,red = 2,yellow = 3。为什么这一点很重要?让我来演示一下。

假设你想打印出颜色 green。为此,你需要添加以下代码行:

print(color[1])

那么我们的程序就会变成这样:

# 将我们的变量定义为一个列表
color = ['blue', 'green', 'red', 'yellow']

# 打印出一种颜色
print(color[1])

当你运行该程序时,它会输出颜色 green

我们再进一步。假设你还想定义一个与颜色对应的水果变量。于是我们可以这样定义:

fruit = ['blueberry', 'apple', 'cherry', 'banana']

你应该已经看出接下来的发展方向了。要打印出 green apple,你可以使用以下语句:

print(color[1], fruit[1])

这里我们分别打印了位置 1 的颜色值和位置 1 的水果值。

是不是很巧妙?

那如果我们想把所有颜色和所有水果一一对应起来呢?那看起来会是这样:

print(color[0], fruit[0], color[1], fruit[1], color[2], fruit[2], color[3], fruit[3])

完整的脚本如下:

# 将我们的 color 变量定义为一个列表
color = ['blue', 'green', 'red', 'yellow']
# 将我们的 fruit 变量定义为一个列表
fruit = ['blueberry', 'apple', 'cherry', 'banana']

# 打印两个变量
print(color[0], fruit[0], color[1], fruit[1], color[2], fruit[2], color[3], fruit[3])

如果我们运行该程序(例如使用命令 python lists.py),输出结果将会是:

blue blueberry green apple red cherry yellow banana

现在我们来点有挑战性的内容。如果我们不希望输出只有一行,而是希望输出格式如下所示:

   color      fruit
0   blue   blueberry
1  green       apple
2    red      cherry
3 yellow      banana

要实现这种格式化的输出,我们需要在 Python 技能上向前迈出一大步,并引入一个名为 Pandas 的框架。Pandas 是一个用于数据分析和操作的框架。要使用 Pandas,首先需要安装它。我这里是在 Ubuntu Linux 20.04 上进行演示,安装 Pandas 的命令如下:

sudo apt-get install python3-pandas -y

一旦框架安装完成,你就可以在程序中使用它了。要在程序中使用 Pandas,我们需要用以下语句导入它:

import pandas as pd

这意味着我们在脚本中将通过 pd 来调用 Pandas 的函数。我们将使用的 Pandas 函数是 DataFrame,它允许你处理二维、大小可变、可能包含不同类型数据的表格型数据。换句话说,它能让你更轻松地处理各种类型的数据。

还在跟着我吗?

到目前为止,我们已经理解的脚本部分如下所示:

# 导入 Pandas 框架,并将其简写为 pd
import pandas as pd

# 将我们的 color 变量定义为一个列表
color = ['blue', 'green', 'red', 'yellow']
# 将我们的 fruit 变量定义为一个列表
fruit = ['blueberry', 'apple', 'cherry', 'banana']

一切顺利。现在进入更具挑战性的部分。我们要做的第一件事是使用 colorfruit 这两个变量作为列,通过 DataFrame 函数定义一个新变量。这个新变量将被命名为 df。记住,我们通过 pd 调用 Pandas,而使用的函数是 DataFrame。这一行代码如下所示:

df = pd.DataFrame(columns=['color', 'fruit'])

所以,我们的变量是 df,通过 pd 调用 Pandas,使用 DataFrame 函数,并将 colorfruit 格式化为列。

明白了吗?很好。

接下来,我们将使用之前创建的变量为这些列赋值,代码如下:

df['color'], df['fruit'] = color, fruit

最后,我们打印出全部内容:

print(df)

完整的程序如下所示:

# 导入 Pandas 框架,并将其简写为 pd
import pandas as pd

# 将我们的 color 变量定义为一个列表
color = ['blue', 'green', 'red', 'yellow']
# 将我们的 fruit 变量定义为一个列表
fruit = ['blueberry', 'apple', 'cherry', 'banana']

# 将 df 变量定义为包含 color 和 fruit 列的格式化数据
df = pd.DataFrame(columns=['color', 'fruit'])
# 为列 color 和 fruit 赋值
df['color'], df['fruit'] = color, fruit

# 打印全部内容
print(df)

现在,当我们运行该程序时,就会看到我们期望的输出:

   color      fruit
0   blue  blueberry
1  green      apple
2    red     cherry
3 yellow     banana

虽然我们在这里确实提升了复杂度,但你现在已掌握了如何使用外部框架(如 Pandas)来让 Python 变得更加有用和灵活。

你应该已经感觉自己变得更聪明了!