【Python基础知识】Python dict字典介绍

好的!Python **字典(Dictionary)** 是另一种核心数据结构,与列表形成完美互补。如果说列表是"有序的物品排列",字典就是"带标签的储物柜"。

源自与KIMI的对话

---

## Python 字典(Dict)详解

字典是**无序、可变、键值对(key-value)**的映射结构,用花括号 `{}` 表示。

### 1. 创建字典

```python
# 空字典
empty_dict = {}
empty_dict = dict()

# 字面量创建
student = {
    "name": "张三",
    "age": 20,
    "major": "计算机科学",
    "gpa": 3.8
}

# 从键值对序列创建
pairs = [("a", 1), ("b", 2), ("c", 3)]
letter_dict = dict(pairs)
print(letter_dict)  # {'a': 1, 'b': 2, 'c': 3}

# 字典推导式
squares = {x: x**2 for x in range(1, 6)}
print(squares)  # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
```

### 2. 访问与修改

```python
person = {"name": "李四", "age": 25, "city": "北京"}

# 访问(通过键)
print(person["name"])      # 李四
print(person.get("age"))   # 25
print(person.get("phone", "未设置"))  # 未设置(键不存在时返回默认值)

# 修改与添加
person["age"] = 26         # 修改已有键
person["phone"] = "13800138000"  # 添加新键

# 批量更新
person.update({"email": "[email protected]", "city": "上海"})
```

### 3. 删除操作

```python
data = {"a": 1, "b": 2, "c": 3, "d": 4}

# 删除并返回值
val = data.pop("b")        # val=2, data变为{"a": 1, "c": 3, "d": 4}

# 删除并返回最后插入的键值对(Python 3.7+ 保持插入顺序)
last_item = data.popitem()  # ('d', 4)

# 删除指定键
del data["a"]

# 清空
data.clear()               # {}
```

### 4. 遍历字典

```python
scores = {"数学": 90, "英语": 85, "物理": 88, "化学": 92}

# 遍历键(默认)
for subject in scores:
    print(subject)

# 遍历值
for score in scores.values():
    print(score)

# 遍历键值对(最常用)
for subject, score in scores.items():
    print(f"{subject}: {score}分")

# 同时获取索引(enumerate配合)
for idx, (subject, score) in enumerate(scores.items(), 1):
    print(f"{idx}. {subject}: {score}")
```

### 5. 实用技巧与场景

```python
# 示例1:嵌套字典 - 存储复杂信息
students = {
    "2024001": {
        "name": "王五",
        "scores": {"数学": 95, "英语": 88},
        "contact": {"phone": "139xxxx", "email": "[email protected]"}
    },
    "2024002": {
        "name": "赵六", 
        "scores": {"数学": 90, "英语": 92},
        "contact": {"phone": "138xxxx", "email": "[email protected]"}
    }
}

# 访问嵌套数据
print(students["2024001"]["scores"]["数学"])  # 95

# 示例2:统计频率 - 经典用法
text = "hello world hello python"
word_count = {}

for word in text.split():
    word_count[word] = word_count.get(word, 0) + 1
print(word_count)  # {'hello': 2, 'world': 1, 'python': 1}

# 更优雅的写法(使用 collections.Counter)
from collections import Counter
word_count = Counter(text.split())
print(dict(word_count))  # 同上

# 示例3:按值排序字典
scores = {"Alice": 85, "Bob": 92, "Charlie": 78, "David": 95}

# 按分数降序排序(返回列表)
sorted_by_score = sorted(scores.items(), key=lambda x: x[1], reverse=True)
print(sorted_by_score)  # [('David', 95), ('Bob', 92), ('Alice', 85), ('Charlie', 78)]

# 转回字典(Python 3.7+ 保持排序顺序)
rank_dict = dict(sorted_by_score)
```

### 6. 列表 vs 字典 对比

| 特性 | 列表 `[]` | 字典 `{}` |
|------|----------|----------|
| **访问方式** | 索引 `[0]` | 键 `["key"]` |
| **查找速度** | O(n) 慢 | O(1) 快(哈希表) |
| **有序性** | 有序 | Python 3.7+ 保持插入顺序 |
| **适用场景** | 序列数据、队列 | 映射关系、查表、缓存 |
| **元素要求** | 任意对象 | 键必须不可变(数字、字符串、元组)|

---

## 综合实战:学生成绩管理系统

```python
# 结合列表和字典的实际应用

class_score = {
    "计算机1班": [
        {"学号": "C001", "姓名": "张三", "成绩": {"数学": 90, "英语": 85}},
        {"学号": "C002", "姓名": "李四", "成绩": {"数学": 88, "英语": 92}}
    ],
    "计算机2班": [
        {"学号": "C003", "姓名": "王五", "成绩": {"数学": 95, "英语": 78}},
        {"学号": "C004", "姓名": "赵六", "成绩": {"数学": 82, "英语": 88}}
    ]
}

# 任务:计算每个班级的平均数学成绩
for class_name, students in class_score.items():
    math_scores = [s["成绩"]["数学"] for s in students]
    avg_math = sum(math_scores) / len(math_scores)
    print(f"{class_name} 数学平均分: {avg_math:.2f}")

# 任务:找出全校数学最高分的学生
all_students = [s for students in class_score.values() for s in students]
top_student = max(all_students, key=lambda x: x["成绩"]["数学"])
print(f"数学最高分: {top_student['姓名']} ({top_student['成绩']['数学']}分)")
```

---

## 与AI学习的关联

在机器学习中,字典无处不在:
- **数据集标签**:`{"image_001.jpg": "cat", "image_002.jpg": "dog"}`
- **模型配置**:`{"learning_rate": 0.001, "batch_size": 32, "epochs": 100}`
- **特征工程**:存储特征名称到值的映射

接下来你可以学习 **元组(tuple)**(不可变列表)和 **集合(set)**(去重、交并差运算),它们与列表、字典共同构成 Python 的四大核心数据结构!

Could not load content