在 Python 中,列表(list) 是最常用、最灵活的数据结构之一。 它能存放多个数据,可以增删改查,还能嵌套其他列表,是编程中处理“集合类信息”的核心工具。
本章我们将系统学习列表的定义、操作方法、常用函数与实战应用。
一、什么是列表
列表(list)是一个 有序、可变 的元素集合。 它可以存储任意类型的数据,包括数字、字符串、布尔值、甚至其他列表。
示例:
1numbers = [1, 2, 3, 4, 5] 2fruits = ["apple", "banana", "cherry"] 3mixed = [1, "hello", True, 3.14] 4nested = [1, [2, 3], [4, 5]] 5
二、创建列表的方式
1. 使用方括号 []
1nums = [10, 20, 30, 40] 2
2. 使用内置函数 list()
1nums = list((10, 20, 30)) 2
3. 创建空列表
1empty_list = [] 2# 或 3empty_list = list() 4
三、访问列表元素
列表是有序的,可以通过 索引(index) 访问元素。
1. 正向索引(从 0 开始)
1fruits = ["apple", "banana", "cherry"] 2print(fruits[0]) # 输出 apple 3print(fruits[2]) # 输出 cherry 4
2. 反向索引(从 -1 开始)
1print(fruits[-1]) # 输出 cherry 2print(fruits[-2]) # 输出 banana 3
3. 切片(slice)
可以取出部分元素,形成新的列表:
1print(fruits[0:2]) # ['apple', 'banana'] 2print(fruits[1:]) # ['banana', 'cherry'] 3print(fruits[:2]) # ['apple', 'banana'] 4
四、修改列表内容
1. 直接修改元素
1fruits = ["apple", "banana", "cherry"] 2fruits[1] = "orange" 3print(fruits) 4
输出:
1['apple', 'orange', 'cherry'] 2
2. 替换多个元素
1fruits[0:2] = ["pear", "melon"] 2print(fruits) 3
输出:
1['pear', 'melon', 'cherry'] 2
五、添加元素
1. append() — 在列表末尾添加元素
1fruits = ["apple", "banana"] 2fruits.append("cherry") 3print(fruits) 4
输出:
1['apple', 'banana', 'cherry'] 2
2. insert() — 在指定位置插入元素
1fruits.insert(1, "orange") 2print(fruits) 3
输出:
1['apple', 'orange', 'banana', 'cherry'] 2
3. extend() — 合并另一个列表
1fruits = ["apple", "banana"] 2more = ["cherry", "peach"] 3fruits.extend(more) 4print(fruits) 5
输出:
1['apple', 'banana', 'cherry', 'peach'] 2
六、删除元素
1. del 删除指定位置的元素
1fruits = ["apple", "banana", "cherry"] 2del fruits[1] 3print(fruits) 4
输出:
1['apple', 'cherry'] 2
2. pop() — 删除并返回元素(默认最后一个)
1fruits = ["apple", "banana", "cherry"] 2item = fruits.pop() 3print(item) 4print(fruits) 5
输出:
1cherry 2['apple', 'banana'] 3
3. remove() — 删除指定值的第一个匹配项
1fruits = ["apple", "banana", "apple", "cherry"] 2fruits.remove("apple") 3print(fruits) 4
输出:
1['banana', 'apple', 'cherry'] 2
4. clear() — 清空整个列表
1fruits.clear() 2print(fruits) 3
输出:
1[] 2
七、遍历列表
示例:
1fruits = ["apple", "banana", "cherry"] 2for fruit in fruits: 3 print(fruit) 4
输出:
1apple 2banana 3cherry 4
八、判断与统计
1. 判断元素是否存在
1if "apple" in fruits: 2 print("有苹果") 3
2. 统计元素数量
1count = fruits.count("apple") 2print(count) 3
3. 获取列表长度
1print(len(fruits)) 2
九、排序与反转
1. 升序排序
1nums = [3, 1, 4, 2] 2nums.sort() 3print(nums) 4
输出:
1[1, 2, 3, 4] 2
2. 降序排序
1nums.sort(reverse=True) 2print(nums) 3
输出:
1[4, 3, 2, 1] 2
3. 临时排序(不改变原列表)
1nums = [3, 1, 4, 2] 2print(sorted(nums)) 3print(nums) 4
输出:
1[1, 2, 3, 4] 2[3, 1, 4, 2] 3
4. 反转列表
1nums.reverse() 2print(nums) 3
输出:
1[2, 4, 1, 3] 2
十、列表的复制
1. 浅拷贝(copy)
1a = [1, 2, 3] 2b = a.copy() 3b.append(4) 4print(a, b) 5
输出:
1[1, 2, 3] [1, 2, 3, 4] 2
2. 切片复制
1b = a[:] 2
✅ 注意:浅拷贝不会复制嵌套结构中的子列表。
十一、嵌套列表
列表中可以再包含列表,形成二维或多维结构。
1matrix = [ 2 [1, 2, 3], 3 [4, 5, 6], 4 [7, 8, 9] 5] 6print(matrix[1][2]) # 输出6 7
十二、列表常用内置函数总结
| 函数 | 作用 |
|---|---|
| len(list) | 获取长度 |
| max(list) | 最大值 |
| min(list) | 最小值 |
| sum(list) | 求和(元素为数值时) |
| list.index(x) | 获取元素索引 |
| list.count(x) | 统计出现次数 |
| list.sort() | 排序 |
| list.reverse() | 反转 |
| list.copy() | 浅拷贝 |
十三、实战案例:学生成绩分析
1scores = [85, 92, 78, 90, 88, 76, 95] 2 3# 1. 平均分 4avg = sum(scores) / len(scores) 5 6# 2. 最高分与最低分 7max_score = max(scores) 8min_score = min(scores) 9 10# 3. 排序 11sorted_scores = sorted(scores, reverse=True) 12 13print(f"平均分:{avg:.2f}") 14print(f"最高分:{max_score}") 15print(f"最低分:{min_score}") 16print(f"从高到低排序:{sorted_scores}") 17
输出:
1平均分:86.29 2最高分:95 3最低分:76 4从高到低排序:[95, 92, 90, 88, 85, 78, 76] 5
十四、小结
| 操作类别 | 方法或语法 | 示例 |
|---|---|---|
| 创建 | []、list() | list(range(5)) |
| 访问 | 索引、切片 | a[0]、a[1:3] |
| 修改 | 赋值 | a[1] = 10 |
| 添加 | append()、extend()、insert() | a.append(6) |
| 删除 | pop()、remove()、del | a.pop(0) |
| 遍历 | for | for i in a: |
| 排序 | sort()、sorted() | a.sort() |
| 统计 | len()、sum()、count() | len(a) |
✅ 一句话总结
列表是 Python 最灵活的数据容器,几乎所有程序逻辑都能用它表示。
下一章,我们将学习 元组(tuple), 了解一种不可变但更安全高效的数据结构。
《Python编程实战 · 基础入门篇 | 列表(list)》 是转载文章,点击查看原文。
