永恒极光免安装绿色中文版
646M · 2025-11-11
本章我们将系统学习列表的定义、操作方法、常用函数与实战应用。
列表(list)是一个 有序、可变 的元素集合。 它可以存储任意类型的数据,包括数字、字符串、布尔值、甚至其他列表。
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", True, 3.14]
nested = [1, [2, 3], [4, 5]]
[]nums = [10, 20, 30, 40]
list()nums = list((10, 20, 30))
empty_list = []
# 或
empty_list = list()
列表是有序的,可以通过 索引(index) 访问元素。
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # 输出 apple
print(fruits[2]) # 输出 cherry
print(fruits[-1]) # 输出 cherry
print(fruits[-2]) # 输出 banana
可以取出部分元素,形成新的列表:
print(fruits[0:2]) # ['apple', 'banana']
print(fruits[1:]) # ['banana', 'cherry']
print(fruits[:2]) # ['apple', 'banana']
fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"
print(fruits)
输出:
['apple', 'orange', 'cherry']
fruits[0:2] = ["pear", "melon"]
print(fruits)
输出:
['pear', 'melon', 'cherry']
append() — 在列表末尾添加元素fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
输出:
['apple', 'banana', 'cherry']
insert() — 在指定位置插入元素fruits.insert(1, "orange")
print(fruits)
输出:
['apple', 'orange', 'banana', 'cherry']
extend() — 合并另一个列表fruits = ["apple", "banana"]
more = ["cherry", "peach"]
fruits.extend(more)
print(fruits)
输出:
['apple', 'banana', 'cherry', 'peach']
del 删除指定位置的元素fruits = ["apple", "banana", "cherry"]
del fruits[1]
print(fruits)
输出:
['apple', 'cherry']
pop() — 删除并返回元素(默认最后一个)fruits = ["apple", "banana", "cherry"]
item = fruits.pop()
print(item)
print(fruits)
输出:
cherry
['apple', 'banana']
remove() — 删除指定值的第一个匹配项fruits = ["apple", "banana", "apple", "cherry"]
fruits.remove("apple")
print(fruits)
输出:
['banana', 'apple', 'cherry']
clear() — 清空整个列表fruits.clear()
print(fruits)
输出:
[]
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
输出:
apple
banana
cherry
if "apple" in fruits:
print("有苹果")
count = fruits.count("apple")
print(count)
print(len(fruits))
nums = [3, 1, 4, 2]
nums.sort()
print(nums)
输出:
[1, 2, 3, 4]
nums.sort(reverse=True)
print(nums)
输出:
[4, 3, 2, 1]
nums = [3, 1, 4, 2]
print(sorted(nums))
print(nums)
输出:
[1, 2, 3, 4]
[3, 1, 4, 2]
nums.reverse()
print(nums)
输出:
[2, 4, 1, 3]
a = [1, 2, 3]
b = a.copy()
b.append(4)
print(a, b)
输出:
[1, 2, 3] [1, 2, 3, 4]
b = a[:]
列表中可以再包含列表,形成二维或多维结构。
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[1][2]) # 输出6
| 函数 | 作用 |
|---|---|
len(list) | 获取长度 |
max(list) | 最大值 |
min(list) | 最小值 |
sum(list) | 求和(元素为数值时) |
list.index(x) | 获取元素索引 |
list.count(x) | 统计出现次数 |
list.sort() | 排序 |
list.reverse() | 反转 |
list.copy() | 浅拷贝 |
scores = [85, 92, 78, 90, 88, 76, 95]
# 1. 平均分
avg = sum(scores) / len(scores)
# 2. 最高分与最低分
max_score = max(scores)
min_score = min(scores)
# 3. 排序
sorted_scores = sorted(scores, reverse=True)
print(f"平均分:{avg:.2f}")
print(f"最高分:{max_score}")
print(f"最低分:{min_score}")
print(f"从高到低排序:{sorted_scores}")
输出:
平均分:86.29
最高分:95
最低分:76
从高到低排序:[95, 92, 90, 88, 85, 78, 76]
| 操作类别 | 方法或语法 | 示例 |
|---|---|---|
| 创建 | []、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) |
下一章,我们将学习 元组(tuple), 了解一种不可变但更安全高效的数据结构。