17.1 collections 模块
提供额外的数据结构。
Counter - 计数器
1 | from collections import Counter |
defaultdict - 默认字典
1 | from collections import defaultdict |
deque - 双端队列
1 | from collections import deque |
17.2 itertools 模块
提供高效的迭代工具。
常用函数
1 | import itertools |
17.3 functools 模块
提供高阶函数工具。
lru_cache - 缓存装饰器
1 | from functools import lru_cache |
reduce - 归约函数
1 | from functools import reduce |
17.4 re 模块(正则表达式)
1 | import re |
17.5 os 与 sys 模块
os 模块
1 | import os |
sys 模块
1 | import sys |
17.6 math 与 decimal 模块
math 模块
1 | import math |
decimal 模块(高精度)
1 | from decimal import Decimal |
17.7 本章小结
collections:Counter、defaultdict、dequeitertools:chain、combinations、permutationsfunctools:lru_cache、reducere:正则表达式操作os/sys:系统和路径操作math/decimal:数学运算
练习题
- 使用 Counter 统计文本中单词频率
- 使用 defaultdict 实现电话簿分组
- 使用正则表达式验证邮箱格式
- 使用 lru_cache 优化递归函数性能