Python 常用标准库

17.1 collections 模块

提供额外的数据结构。

Counter - 计数器

1
2
3
4
5
6
7
8
9
10
11
12
13
from collections import Counter

text = "hello world"
counter = Counter(text)
print(counter) # Counter({'l': 3, 'o': 2, ...})

# 最常见元素
print(counter.most_common(3)) # [('l', 3), ('o', 2), ('h', 1)]

# 统计单词
words = ["apple", "banana", "apple", "orange", "banana", "apple"]
word_count = Counter(words)
print(word_count["apple"]) # 3

defaultdict - 默认字典

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from collections import defaultdict

# 普通字典
d = {}
# d["key"] # KeyError

# 默认字典
dd = defaultdict(int) # 默认值为 0
dd["count"] += 1
print(dd["count"]) # 1

# 分组
students = [
("Alice", "A"),
("Bob", "B"),
("Charlie", "A"),
("David", "B")
]

groups = defaultdict(list)
for name, grade in students:
groups[grade].append(name)

print(groups)
# defaultdict(<class 'list'>, {'A': ['Alice', 'Charlie'], 'B': ['Bob', 'David']})

deque - 双端队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from collections import deque

d = deque([1, 2, 3])

d.append(4) # 右端添加
d.appendleft(0) # 左端添加
print(d) # deque([0, 1, 2, 3, 4])

d.pop() # 右端删除
d.popleft() # 左端删除
print(d) # deque([1, 2, 3])

# 限制长度
d = deque(maxlen=3)
d.append(1)
d.append(2)
d.append(3)
d.append(4) # 自动移除最左边
print(d) # deque([2, 3, 4])

17.2 itertools 模块

提供高效的迭代工具。

常用函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import itertools

# count - 无限计数
for i in itertools.count(10, 2):
if i > 20:
break
print(i, end=" ") # 10 12 14 16 18 20

# cycle - 循环
count = 0
for item in itertools.cycle(["A", "B", "C"]):
print(item, end=" ")
count += 1
if count >= 6:
break
# A B C A B C

# repeat - 重复
print(list(itertools.repeat("X", 3))) # ['X', 'X', 'X']

# chain - 链式迭代
for item in itertools.chain([1, 2], [3, 4]):
print(item, end=" ") # 1 2 3 4

# combinations - 组合
print(list(itertools.combinations([1, 2, 3], 2)))
# [(1, 2), (1, 3), (2, 3)]

# permutations - 排列
print(list(itertools.permutations([1, 2, 3])))
# [(1, 2, 3), (1, 3, 2), (2, 1, 3), ...]

17.3 functools 模块

提供高阶函数工具。

lru_cache - 缓存装饰器

1
2
3
4
5
6
7
8
9
10
11
from functools import lru_cache

@lru_cache(maxsize=128)
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(100)) # 瞬间完成
print(fibonacci.cache_info())
# CacheInfo(hits=196, misses=101, maxsize=128, currsize=101)

reduce - 归约函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from functools import reduce

# 求和
numbers = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, numbers)
print(total) # 15

# 求最大值
max_val = reduce(lambda x, y: x if x > y else y, numbers)
print(max_val) # 5

# 字符串拼接
words = ["Hello", "World", "Python"]
sentence = reduce(lambda x, y: x + " " + y, words)
print(sentence) # Hello World Python

17.4 re 模块(正则表达式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import re

text = "Email: test@example.com, Phone: 123-456-7890"

# 查找
email = re.search(r"\w+@\w+\.\w+", text)
print(email.group()) # test@example.com

# 查找所有
numbers = re.findall(r"\d+", text)
print(numbers) # ['123', '456', '7890']

# 替换
result = re.sub(r"\d+", "XXX", text)
print(result) # Email: test@example.com, Phone: XXX-XXX-XXX

# 分割
words = re.split(r"[,\s]+", "one,two three four")
print(words) # ['one', 'two', 'three', 'four']

17.5 os 与 sys 模块

os 模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import os

# 路径操作
print(os.getcwd()) # 当前目录
print(os.listdir(".")) # 列出文件
print(os.path.exists("file.txt"))
print(os.path.isfile("file.txt"))
print(os.path.isdir("dir"))

# 创建/删除
os.makedirs("test/nested", exist_ok=True)
os.rmdir("test/nested")

# 环境变量
print(os.environ.get("PATH"))

sys 模块

1
2
3
4
5
6
7
8
9
import sys

print(sys.version) # Python 版本
print(sys.platform) # 操作系统
print(sys.argv) # 命令行参数
print(sys.path) # 模块搜索路径

# 退出
# sys.exit(0)

17.6 math 与 decimal 模块

math 模块

1
2
3
4
5
6
7
8
9
import math

print(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045
print(math.sqrt(16)) # 4.0
print(math.ceil(3.2)) # 4
print(math.floor(3.8)) # 3
print(math.factorial(5)) # 120
print(math.log(100, 10)) # 2.0

decimal 模块(高精度)

1
2
3
4
5
6
7
8
9
from decimal import Decimal

# 浮点数精度问题
print(0.1 + 0.2) # 0.30000000000000004

# Decimal 解决
a = Decimal("0.1")
b = Decimal("0.2")
print(a + b) # 0.3

17.7 本章小结

  • collections:Counter、defaultdict、deque
  • itertools:chain、combinations、permutations
  • functools:lru_cache、reduce
  • re:正则表达式操作
  • os/sys:系统和路径操作
  • math/decimal:数学运算

练习题

  1. 使用 Counter 统计文本中单词频率
  2. 使用 defaultdict 实现电话簿分组
  3. 使用正则表达式验证邮箱格式
  4. 使用 lru_cache 优化递归函数性能
ByteFisher
分享编程技术 · 记录钓鱼乐趣
扫码关注
▸ 扫码关注 ◂
Python基础教程 系列
第 17/20 篇
分享: