CHAPTER 03
数据结构:列表、字典、元组与集合
Python 的容器类型,用于组织和存储多个数据。AI 中的训练数据、配置参数、模型结果,都用这些结构来管理。
list —— 列表(有序可变)
列表是 Python 最常用的数据结构。用方括号 [] 定义,可以存任意类型的数据,有顺序、可修改。
核心概念
索引(Index)从 0 开始
列表中每个元素都有一个位置编号,从 0 开始(不是 1)。第一个元素是 list[0],第二个是 list[1]。Python 还支持负数索引,list[-1] 是最后一个元素,非常方便。
PYTHON · list 基础
# 创建列表
losses = [2.5, 1.8, 1.2, 0.9, 0.7] # 每轮的损失值
labels = ["cat", "dog", "bird"] # 分类标签
mixed = [1, "hello", 3.14, True] # 可以存不同类型
# 索引访问
print(losses[0]) # 2.5(第一个)
print(losses[2]) # 1.2(第三个)
print(losses[-1]) # 0.7(最后一个)
print(losses[-2]) # 0.9(倒数第二个)
# 切片(slice):取一段数据
print(losses[1:3]) # [1.8, 1.2](索引1到2,不含3)
print(losses[:3]) # [2.5, 1.8, 1.2](前3个)
print(losses[2:]) # [1.2, 0.9, 0.7](从第3个到结束)
print(losses[::-1]) # [0.7, 0.9, 1.2, 1.8, 2.5](反转)
PYTHON · list 操作方法
history = [2.5, 1.8] # 训练历史
# 添加元素
history.append(1.2) # 末尾添加:[2.5, 1.8, 1.2]
history.insert(0, 3.0) # 指定位置插入:[3.0, 2.5, 1.8, 1.2]
history.extend([0.9, 0.7]) # 合并另一个列表
# 删除元素
history.remove(3.0) # 删除第一个值为3.0的元素
last = history.pop() # 弹出并返回最后一个元素
# 查询
print(len(history)) # 元素数量
print(min(history)) # 最小值(最好的 loss)
print(max(history)) # 最大值
print(sum(history)) # 求和
print(sum(history)/len(history)) # 平均值
# 排序
history.sort() # 原地排序(升序)
history.sort(reverse=True) # 降序
sorted_h = sorted(history) # 返回新列表,不修改原列表
# 检查元素是否在列表中
print(0.9 in history) # True
AI 中的列表
在 AI 开发中,列表无处不在:train_losses = [] 记录训练损失曲线,messages = [{"role": "user", "content": "..."}] 构建对话历史,predictions = [] 收集模型预测结果。
列表推导式(List Comprehension)
这是 Python 的特色语法,可以用一行代码从现有列表创建新列表,比循环更简洁。在数据预处理中大量使用。
PYTHON · 列表推导式
# 普通写法(循环)
squares = []
for x in range(5):
squares.append(x ** 2)
# 列表推导式(一行)= [表达式 for 变量 in 可迭代对象]
squares = [x ** 2 for x in range(5)]
print(squares) # [0, 1, 4, 9, 16]
# 带条件筛选
even = [x for x in range(10) if x % 2 == 0]
print(even) # [0, 2, 4, 6, 8]
# AI 实战:归一化数据到 [0, 1]
raw_scores = [10, 20, 30, 40, 50]
max_score = max(raw_scores)
normalized = [s / max_score for s in raw_scores]
print(normalized) # [0.2, 0.4, 0.6, 0.8, 1.0]
# 批量处理文本(NLP 预处理)
texts = ["Hello World", " Python ", "AI Dev"]
cleaned = [t.strip().lower() for t in texts]
print(cleaned) # ['hello world', 'python', 'ai dev']
dict —— 字典(键值对映射)
字典用 {} 定义,存储键(key)→ 值(value)的映射关系。就像真实的字典:通过词条(key)找到解释(value)。在 AI 开发中,字典是最重要的数据结构之一,API 请求、配置参数、模型结果都用字典表示。
PYTHON · dict 基础
# 创建字典:{ key: value, key: value, ... }
model_config = {
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"temperature": 0.7,
"stream": False
}
# 访问值(用 key 取 value)
print(model_config["model"]) # claude-sonnet-4-6
print(model_config["temperature"]) # 0.7
# 安全访问(不存在的 key 用 get,不会报错)
print(model_config.get("timeout")) # None(key不存在)
print(model_config.get("timeout", 30)) # 30(设置默认值)
# 添加/修改
model_config["top_p"] = 0.9 # 添加新键
model_config["temperature"] = 0.5 # 修改已有键
# 删除
del model_config["stream"] # 删除 key
val = model_config.pop("top_p") # 删除并返回值
# 查询
print("model" in model_config) # True(检查 key 是否存在)
print(model_config.keys()) # 所有 key
print(model_config.values()) # 所有 value
print(model_config.items()) # 所有 (key, value) 对
PYTHON · AI 场景中的字典
# LLM API 消息格式(最常见的字典用法)
messages = [
{"role": "system", "content": "你是一个 Python 专家"},
{"role": "user", "content": "如何学习 Python"}
]
# 模型训练结果统计
metrics = {
"train_loss": 0.23,
"val_loss": 0.31,
"accuracy": 0.94,
"f1_score": 0.92
}
# 遍历字典
for metric_name, value in metrics.items():
print(f"{metric_name}: {value}")
# train_loss: 0.23
# val_loss: 0.31 ...
# 字典推导式
raw = {"a": 1, "b": 2, "c": 3}
doubled = {k: v * 2 for k, v in raw.items()}
print(doubled) # {'a': 2, 'b': 4, 'c': 6}
# 合并字典(Python 3.9+)
base_config = {"model": "gpt-4o", "temperature": 0.7}
extra = {"max_tokens": 2048}
full_config = base_config | extra # 合并
tuple —— 元组(有序不可变)
元组用圆括号 () 定义,和列表类似,但不能修改(不可变)。一旦创建,元素就固定了。适合存储不应该被改变的数据,比如坐标、RGB 颜色值、图像尺寸。
PYTHON · tuple 元组
# 创建元组
image_size = (224, 224, 3) # (高, 宽, 通道) 图像尺寸
point = (3.5, 2.1) # 2D 坐标
rgb = (255, 128, 0) # 橙色的 RGB 值
# 访问元素(和列表一样用索引)
print(image_size[0]) # 224(高度)
print(image_size[2]) # 3(通道数)
# 元组不能修改(这行会报错!)
# image_size[0] = 256 ← TypeError!
# 解包(unpack):把元组拆分成多个变量
height, width, channels = image_size
print(f"图像: {height}x{width}, {channels}通道")
# 函数返回多个值时,实际上是返回一个元组
def get_model_stats():
return 0.95, 0.23 # 返回 (accuracy, loss)
acc, loss = get_model_stats() # 解包接收
print(f"准确率: {acc}, 损失: {loss}")
set —— 集合(无序不重复)
集合用 {}(但里面没有键值对)定义,自动去除重复元素,无固定顺序。适合去重、快速判断元素是否存在、以及集合运算(交集、并集)。
PYTHON · set 集合
# 创建集合(自动去重)
vocab = {"hello", "world", "hello", "python"}
print(vocab) # {'hello', 'world', 'python'}(去重了)
# 用列表创建集合(去重技巧)
labels = ["cat", "dog", "cat", "bird", "dog"]
unique_labels = set(labels) # {'cat', 'dog', 'bird'}
print(len(unique_labels)) # 3 个唯一类别
# 检查元素(速度比列表快很多)
print("cat" in unique_labels) # True
# 集合运算(在 NLP 词汇分析中常用)
vocab_a = {"apple", "banana", "cat"}
vocab_b = {"cat", "dog", "banana"}
print(vocab_a & vocab_b) # 交集:{'cat', 'banana'}(共有的)
print(vocab_a | vocab_b) # 并集:{'apple', 'banana', 'cat', 'dog'}
print(vocab_a - vocab_b) # 差集:{'apple'}(A 有 B 没有)
四种数据结构对比
| 类型 | 符号 | 有序 | 可修改 | 允许重复 | AI 中的典型用途 |
|---|---|---|---|---|---|
list |
[] |
✅ | ✅ | ✅ | 训练损失历史、消息列表、数据集 |
dict |
{k:v} |
✅(3.7+) | ✅ | key唯一 | API 参数、配置文件、模型输出 |
tuple |
() |
✅ | ❌ | ✅ | 图像尺寸、坐标、函数多返回值 |
set |
{} |
❌ | ✅ | ❌ | 词汇表、去重、类别标签集合 |