> **GESP等级：三级 | 考级考点：GESP Python 三级**

# 教师参考答案 — 第2节 列表操作

---

## 一、教案核心要点

### GESP考点对照

| GESP考点 | 知识点 | 示例 |
|---------|--------|------|
| #25 列表添加元素 | append / insert | `colors.append("pink")` / `colors.insert(1,"x")` |
| #26 列表删除元素 | pop / remove / del / clear | `colors.pop()` / `colors.remove("x")` / `del colors[1]` / `colors.clear()` |

### 记忆钩子（建议课堂反复强调）
```
append  追加尾    → 末尾追加
insert  插中间    → 指定位置插入
pop     弹最后    → 弹出（有返回值）
remove  删指定    → 按值删除第一个
del     按索引删  → 语句删除
clear   全清空    → 清空列表
len     查长度    → 获取元素个数
```

---

## 二、演示代码运行结果

### `01_colors_warehouse.py` 运行结果

```
==================================================
  颜色仓库管理系统
==================================================

【初始货架】['red', 'blue', 'green', 'yellow']
  货架长度（颜色数量）: 4

----------------------------------------
① append — 在末尾追加一个颜色
----------------------------------------
  colors.append('pink')  →  ['red', 'blue', 'green', 'yellow', 'pink']
  长度: 5

----------------------------------------
② insert — 在指定位置插入一个颜色
----------------------------------------
  colors.insert(2, 'black')  →  ['red', 'blue', 'black', 'green', 'yellow', 'pink']
  注意：原来索引2的'green'被挤到索引3了
  colors.insert(0, 'white')  →  ['white', 'red', 'blue', 'black', 'green', 'yellow', 'pink']

----------------------------------------
③ pop — 弹出（拿走）一个颜色
----------------------------------------
  colors.pop()  →  弹出: 'pink'
  pop后货架: ['white', 'red', 'blue', 'black', 'green', 'yellow']
  colors.pop(1)  →  弹出索引1: 'red'
  pop后货架: ['white', 'blue', 'black', 'green', 'yellow']

----------------------------------------
④ remove — 按颜色名称删除
----------------------------------------
  colors.remove('blue')  →  ['white', 'black', 'green', 'yellow']
  注意：remove 无返回值，只删除第一个匹配的

----------------------------------------
⑤ del — 按索引删除（语句）
----------------------------------------
  del colors[1]  →  ['white', 'green', 'yellow']
  注意：del 是语句，不是方法，所以没有 . 号

----------------------------------------
⑥ 修改——直接赋值
----------------------------------------
  colors[0] = 'dark_red'  →  ['dark_red', 'green', 'yellow']

----------------------------------------
⑦ clear — 清空货架
----------------------------------------
  colors.clear()  →  []
  清空后长度: 0

==================================================
  知识点总结
==================================================
  append(值)   → 末尾追加
  insert(索引,值) → 指定位置插入
  pop(索引)    → 弹出（有返回值）
  remove(值)   → 按值删除第一个
  del 列表[索引] → 语句删除
  clear()      → 清空
  len(列表)    → 长度
==================================================
```

---

## 三、实操代码答案

### 实操①：操作追踪填空 (`02_list_operation_tracking.py`)

```python
# 第1题
colors.append("yellow")

# 第2题
colors.insert(1, "black")

# 第3题
last = colors.pop()

# 第4题
first = colors.pop(0)

# 第5题
colors.remove("blue")

# 第6题
del colors[1]

# 第7题
colors.clear()

# 第8题
count = len(colors)
```

### 实操②：颜色库管理 (`03_color_library_manager.py`)

```python
def add_color(color):
    color_library.append(color)

def insert_color(index, color):
    color_library.insert(index, color)

def remove_color(color):
    color_library.remove(color)

def modify_color(index, new_color):
    color_library[index] = new_color

def pop_last():
    return color_library.pop()
```

**运行结果：**
```
【初始状态】
  当前颜色库: ['red', 'blue', 'green', 'yellow']  |  数量: 4

【1】添加颜色 pink
  当前颜色库: ['red', 'blue', 'green', 'yellow', 'pink']  |  数量: 5

【2】在索引 2 处插入 black
  当前颜色库: ['red', 'blue', 'black', 'green', 'yellow', 'pink']  |  数量: 6

【3】删除颜色 blue
  当前颜色库: ['red', 'black', 'green', 'yellow', 'pink']  |  数量: 5

【4】修改索引 0 为 dark_red
  当前颜色库: ['dark_red', 'black', 'green', 'yellow', 'pink']  |  数量: 5

【5】弹出最后一个颜色
  弹出的颜色: pink
  当前颜色库: ['dark_red', 'black', 'green', 'yellow']  |  数量: 4
```

### 实操③：撤销功能 (`04_undo_with_pop_append.py`)

```python
def add(color):
    color_list.append(color)
    history.append(color)

def undo():
    if history:
        last = history.pop()
        color_list.remove(last)
        print(f"  撤销: {last}")
    else:
        print("  没有可撤销的操作")
```

**运行结果：**
```
【开始添加颜色】
  当前颜色: ['red', 'blue', 'green']

【撤销一次】
  撤销: green
  当前颜色: ['red', 'blue']

【撤销第二次】
  撤销: blue
  当前颜色: ['red']

【再添加两个颜色】
  当前颜色: ['red', 'yellow', 'pink']

【撤销一次】
  撤销: pink
  当前颜色: ['red', 'yellow']

【连续撤销到空】
  撤销: yellow
  撤销: red
  当前颜色: (空)

【空列表时尝试撤销】
  没有可撤销的操作
```

---

## 四、课堂练习册答案

### 练习一：方法连线

| 操作描述 | 方法/语句 |
|---------|----------|
| 在末尾加一个元素 | `append()` |
| 在指定位置加一个元素 | `insert()` |
| 弹出最后一个元素（有返回值） | `pop()` |
| 按值删除第一个匹配项 | `remove()` |
| 按索引删除元素 | `del` |
| 清空整个列表 | `clear()` |
| 获取列表元素个数 | `len()` |

### 练习二：看代码写结果

**题目1：** `['apple', 'banana', 'cherry', 'durian']`

**题目2：** `[10, 20, 25, 30, 40]`

**题目3：**
```
green
['red', 'blue']
```

**题目4：** `['cat', 'bird', 'dog']`

**题目5：** `['a', 'c', 'd']`

**题目6：** `0`

### 练习三：找错改错

**题目1：** 参数顺序反了
```python
# 改正：
colors.insert(2, "yellow")
```

**题目2：** remove 缺少参数
```python
# 改正：
nums.remove(10)
```

**题目3：** append 只能接受一个参数
```python
# 改正：
fruits.append("cherry")
fruits.append("durian")
# 或
fruits.extend(["cherry", "durian"])
```

**题目4：** del 是语句，不是方法
```python
# 改正：
del items[1]
```

### 练习四：补全代码

```python
def do(action):
    actions.append(action)
    history.append(action)
    print(f"执行: {action}")

def undo():
    if history:
        last = history.pop()
        actions.remove(last)
        print(f"撤销: {last}")
    else:
        print("没有可撤销的操作")
```

---

## 五、课后练习卡答案

### 选择题
1. **B** (`append()`)
2. **C** (会返回被删除的元素)
3. **A** (`[1, 2, 4]`)
4. **B** (`del list[0]`)

### 填空题
1. `append`
2. `insert`
3. 最后
4. 一（只删第一个匹配）
5. `clear`
6. `len`

### 写出输出
1. `['apple', 'banana', 'cherry']`
2. `30` 然后 `[10, 20]`
3. `['cat', 'bird']`
4. `3`

### 简答题参考要点

**append 和 insert 的区别：**
- `append(值)` 是把元素**追加到列表末尾**，不关心索引位置
- `insert(索引, 值)` 是把元素**插入到指定的索引位置**，原来该位置及之后的元素后移
- 举例：`colors.append("pink")` 永远加在最后；`colors.insert(0, "pink")` 可以加在最前面

### 附加题答案

```python
def add_plate(color):
    plates.append(color)

def remove_plate():
    if plates:
        plates.pop()
    else:
        print("没有盘子可拿")
```

---

## 六、常见学生错误与纠正

| 错误表现 | 原因 | 纠正方法 |
|---------|------|---------|
| 写成 `list.insert(值, 索引)` | insert 参数顺序记反 | 口诀："先告诉我在哪插，再告诉我插什么" |
| 用 `list.remove(索引)` | 混淆 remove 和 pop | remove 按**值**删，pop 按**索引**删 |
| 写成 `list.del[0]` | 把 del 当成方法 | del 是**语句**，不加 `.()` |
| 用 `list.append(值1, 值2)` | 认为 append 能加多个 | append 只接受一个参数；多个用 `extend` 或多次 append |
| 忘记 `pop()` 有返回值 | 混淆 remove 和 pop | pop = 拿下来（手里有东西），remove = 擦掉（手里没东西） |

---

## 七、扩展建议

### 课后追问
- "如果我想在列表**最前面**插入一个元素，怎么写？" → `insert(0, 值)`
- "如果我想删除列表**最后三个**元素，怎么写？" → `del colors[-3:]`
- "pop(0) 和 remove(colors[0]) 的区别？" → 前者返回被删的值且按索引删，后者按值删且不返回

### 游戏化建议
- 让两个学生上台，扮演"列表元素"，其他同学喊 append / insert / pop / remove，台上的"元素"按指令移动，具象化理解列表操作
