{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fd320174",
   "metadata": {},
   "source": [
    "# 第 5 天｜列表、字典与真题数据结构\n",
    "\n",
    "**日期：** 2026 年 7 月 24 日  \n",
    "**核心内容：** 列表、字典、嵌套结构、`for` 遍历和 `if` 筛选\n",
    "\n",
    "三套真题的数据结构基础：\n",
    "\n",
    "- HTML 表格最终转换为 DataFrame；\n",
    "- API JSON 常表现为嵌套字典和列表；\n",
    "- 多条商品记录常表示为“列表中放字典”。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dae600a3",
   "metadata": {},
   "source": [
    "## 列表：按顺序保存多个值\n",
    "\n",
    "列表使用方括号 `[]`，元素之间用英文逗号分隔。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22081c4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "双肩包\n",
      "拉杆箱\n",
      "帆布包\n"
     ]
    }
   ],
   "source": [
    "product_names = [\"双肩包\", \"拉杆箱\", \"帆布包\"]\n",
    "\n",
    "print(product_names[0])\n",
    "print(product_names[1])\n",
    "print(product_names[2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40e360ea",
   "metadata": {},
   "source": [
    "列表下标从 0 开始：\n",
    "\n",
    "| 人的顺序 | Python 下标 |\n",
    "|---|---:|\n",
    "| 第 1 个 | 0 |\n",
    "| 第 2 个 | 1 |\n",
    "| 第 3 个 | 2 |\n",
    "\n",
    "三个元素的合法下标是 0、1、2。访问下标 3 会触发 `IndexError`。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2d91d43",
   "metadata": {},
   "source": [
    "## 字典：用字段名保存一条记录\n",
    "\n",
    "字典使用花括号 `{}`，每个字段由“键: 值”组成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16854e64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "双肩包\n",
      "125\n",
      "199.0\n"
     ]
    }
   ],
   "source": [
    "product = {\n",
    "    \"品名\": \"双肩包\",\n",
    "    \"销售数量\": 125,\n",
    "    \"商城价格\": 199.0,\n",
    "}\n",
    "\n",
    "print(product[\"品名\"])\n",
    "print(product[\"销售数量\"])\n",
    "print(product[\"商城价格\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6a269d4",
   "metadata": {},
   "source": [
    "| 概念 | 示例 |\n",
    "|---|---|\n",
    "| 键 | `\"销售数量\"` |\n",
    "| 值 | `125` |\n",
    "| 按键取值 | `product[\"销售数量\"]` |\n",
    "| 修改值 | `product[\"销售数量\"] = 130` |\n",
    "\n",
    "键名必须完全匹配。`\"销售数量\"`、`\"销量\"`、`\"销售数量 \"` 是三个不同的键。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bf71723",
   "metadata": {},
   "source": [
    "## 列表中放字典：表示多条记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30c83aec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "双肩包\n",
      "80\n"
     ]
    }
   ],
   "source": [
    "products = [\n",
    "    {\"品名\": \"双肩包\", \"销售数量\": 125, \"商城价格\": 199.0},\n",
    "    {\"品名\": \"帆布包\", \"销售数量\": 80, \"商城价格\": 59.0},\n",
    "]\n",
    "\n",
    "print(products[0][\"品名\"])\n",
    "print(products[1][\"销售数量\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99457b06",
   "metadata": {},
   "source": [
    "`products[0][\"品名\"]` 分两步：\n",
    "\n",
    "1. `products[0]` 取得第 1 条商品字典；\n",
    "2. `[\"品名\"]` 再从该字典取得品名。\n",
    "\n",
    "名仕 API 的 `res_data_02[\"data\"][\"list\"]` 也是逐层进入嵌套结构。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8bc13d1",
   "metadata": {},
   "source": [
    "## `for` 遍历每条商品记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aca56ecd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "双肩包：24875.00 元\n",
      "帆布包：4720.00 元\n"
     ]
    }
   ],
   "source": [
    "for product in products:\n",
    "    total_sales = product[\"销售数量\"] * product[\"商城价格\"]\n",
    "    print(f\"{product['品名']}：{total_sales:.2f} 元\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0647f788",
   "metadata": {},
   "source": [
    "`for product in products:` 的含义：\n",
    "\n",
    "> 依次从 `products` 列表取出一条字典，把当前字典暂时命名为 `product`。\n",
    "\n",
    "| 轮次 | 当前商品 | 计算 |\n",
    "|---:|---|---:|\n",
    "| 1 | 双肩包 | 125 × 199.0 |\n",
    "| 2 | 帆布包 | 80 × 59.0 |\n",
    "\n",
    "循环体必须缩进。循环外的代码只在循环结束后执行一次。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17b2dbef",
   "metadata": {},
   "source": [
    "## `if` 筛选销售数量大于 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "655a1bc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "双肩包 24875.0\n"
     ]
    }
   ],
   "source": [
    "for product in products:\n",
    "    if product[\"销售数量\"] > 100:\n",
    "        total_sales = product[\"销售数量\"] * product[\"商城价格\"]\n",
    "        print(product[\"品名\"], total_sales)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57f2f3c3",
   "metadata": {},
   "source": [
    "`if` 只允许条件为 `True` 的记录执行内部代码。\n",
    "\n",
    "两级缩进分别表示：\n",
    "\n",
    "1. `if` 属于 `for`；\n",
    "2. 计算和输出属于 `if`。\n",
    "\n",
    "科图题中的商品数量筛选、青娅题中的项目筛选，本质上都是根据条件保留记录。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebc225a2",
   "metadata": {},
   "source": [
    "## 与三套真题的关系\n",
    "\n",
    "### 名仕\n",
    "\n",
    "```python\n",
    "result[\"data\"][\"list\"]\n",
    "result[\"data\"][\"hasNextPage\"]\n",
    "```\n",
    "\n",
    "考查嵌套字典、列表和分页布尔值。\n",
    "\n",
    "### 科图、青娅\n",
    "\n",
    "`pd.read_html()` 返回 DataFrame 列表，需要遍历和合并。\n",
    "\n",
    "### pandas\n",
    "\n",
    "```python\n",
    "pd.DataFrame(products)\n",
    "```\n",
    "\n",
    "可以把“列表中的商品字典”转换成二维表格。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d2c14c9",
   "metadata": {},
   "source": [
    "## 常见错误\n",
    "\n",
    "| 错误 | 典型报错 |\n",
    "|---|---|\n",
    "| 列表下标越界 | `IndexError` |\n",
    "| 字典键不存在 | `KeyError` |\n",
    "| 使用错误层级取值 | `TypeError` 或 `KeyError` |\n",
    "| `for` 后漏冒号 | `SyntaxError` |\n",
    "| 循环体未缩进 | `IndentationError` |\n",
    "| `if` 边界符号错误 | 结果数量不正确 |\n",
    "\n",
    "## 综合练习\n",
    "\n",
    "使用 `学生练习.py` 完成列表取值、字典取值，并根据商品数量计算销售总价。完成后尝试口头解释 `products[0][\"品名\"]` 的两层取值过程。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3"
  },
  "title": "第 5 天｜列表、字典与真题数据结构"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
