Python 基础
第十章 · Python 标准库
配合机器学习系列课程
本章目录
2. random 库
随机数生成、随机选择、打乱顺序
3. collections 库
Counter、defaultdict、deque、namedtuple
4. itertools 库
无限迭代器、排列组合、分组
time 库 — 时间处理
-
time.time()
返回当前时间戳(秒,浮点数)
-
time.sleep(n)
暂停执行 n 秒
-
time.localtime()
将时间戳转为本地时间结构体
-
time.strftime()
格式化时间为字符串
import time
now = time.time()
print(now)
formatted = time.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)
start = time.time()
for i in range(1000000):
pass
print(f"耗时: {time.time() - start:.4f} 秒")
print("3秒后开始...")
time.sleep(3)
time 库 — 结构化时间与字符串转换
struct_time 结构体
tm_year, tm_mon, tm_mday
tm_hour, tm_min, tm_sec
tm_wday(周一为0), tm_yday
常用格式符
%Y — 四位年份,%m — 月份
%d — 日期,%H — 24小时
%M — 分钟,%S — 秒
import time
ts = time.time()
st = time.localtime(ts)
print(st.tm_year, st.tm_mon, st.tm_mday)
str_time = "2025-04-23 18:30:00"
st = time.strptime(str_time, "%Y-%m-%d %H:%M:%S")
print(st)
ts = time.mktime(st)
print(ts)
random 库 — 随机数生成
-
random.random()
— [0, 1) 范围内的随机浮点数
-
random.randint(a, b)
— [a, b] 范围内的随机整数
-
random.uniform(a, b)
— [a, b] 范围内的随机浮点数
-
random.choice(seq)
— 从序列中随机选择一个元素
import random
print(random.random())
print(random.uniform(10, 20))
print(random.randint(1, 6))
print(random.randrange(0, 100, 2))
cards = ["A", "2", "3", "4", "5"]
print(random.choice(cards))
random.seed(42)
print(random.random())
random 库 — 采样与打乱
random.shuffle(list)
原地打乱列表顺序
不返回新列表
适用于洗牌、随机排序
random.sample(pop, k)
从总体中无放回地抽取 k 个
返回新列表,不改变原序列
适用于抽样、抽奖
import random
deck = list(range(1, 53))
random.shuffle(deck)
print(deck[:5])
students = ["Alice", "Bob", "Charlie", "David", "Eve"]
winners = random.sample(students, 3)
print(winners)
choices = ["A", "B", "C"]
weights = [0.7, 0.2, 0.1]
result = random.choices(choices, weights=weights, k=10)
print(result)
collections — Counter 计数器
-
Counter
是 dict 的子类,用于统计可哈希对象的频次
- 自动为不存在的键返回 0,不会 KeyError
- 支持数学运算:+、-、&、|
from collections import Counter
text = "hello world"
counts = Counter(text)
print(counts)
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
word_counts = Counter(words)
print(word_counts.most_common(2))
word_counts.update(["apple", "date"])
print(word_counts["apple"])
collections — defaultdict
-
defaultdict
为不存在的键自动提供默认值
- 传入工厂函数:list、dict、set、int 等
- 免去手动检查键是否存在的繁琐代码
from collections import defaultdict
groups = defaultdict(list)
students = [("A", 85), ("B", 92), ("A", 78), ("B", 88)]
for name, score in students:
groups[name].append(score)
print(dict(groups))
counts = defaultdict(int)
for char in "hello":
counts[char] += 1
print(dict(counts))
collections — deque 双端队列
deque 优势
两端插入/删除都是 O(1)
列表头部操作是 O(n)
适用于队列、滑动窗口
常用方法
append() / pop() — 右端
appendleft() / popleft() — 左端
rotate(n) — 循环旋转
from collections import deque
dq = deque([1, 2, 3])
dq.append(4)
dq.appendleft(0)
dq.pop()
dq.popleft()
nums = [1, 3, -1, -3, 5, 3, 6, 7]
window = deque()
for i, num in enumerate(nums):
if window and window[0] <= i - 3:
window.popleft()
while window and nums[window[-1]] < num:
window.pop()
window.append(i)
collections — namedtuple 命名元组
-
namedtuple
创建带有命名字段的元组子类
- 兼具元组的不可变性和类的可读性
- 内存占用与普通元组相同,比字典更省空间
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(10, 20)
print(p.x, p.y)
print(p[0], p[1])
Student = namedtuple("Student", ["name", "age", "score"])
s = Student("Alice", 20, 92)
print(f"{s.name} 的成绩是 {s.score}")
data = {"x": 1, "y": 2}
p2 = Point(**data)
itertools — 迭代器工具箱
无限迭代器
count(start, step)
cycle(iterable)
repeat(elem, n)
组合迭代器
permutations(p, r) — 排列
combinations(p, r) — 组合
product(p, q) — 笛卡尔积
from itertools import count, cycle, permutations, combinations
for i in count(10, 2):
if i > 20:
break
print(i, end=" ")
items = ["A", "B", "C"]
for p in permutations(items, 2):
print(p, end=" ")
for c in combinations(items, 2):
print(c, end=" ")
itertools — 分组与拉链
-
groupby()
— 按相同键值对连续元素分组(需先排序)
-
zip()
— 并行遍历多个序列
-
chain()
— 将多个迭代器串接为一个
-
islice()
— 对迭代器进行切片
from itertools import count, groupby, chain, islice
data = [("A", 1), ("A", 2), ("B", 3), ("B", 4)]
for key, group in groupby(data, key=lambda x: x[0]):
print(f"{key}: {list(group)}")
all_nums = list(chain([1, 2], [3, 4], [5, 6]))
print(all_nums)
for x in islice(count(), 5, 10):
print(x, end=" ")
本章总结
▸ time:时间戳、格式化、计时器,掌握 strftime / strptime
▸ random:随机数、抽样、打乱,设置种子保证可复现
▸ collections:Counter 计数、defaultdict 自动默认值、deque 高效队列、namedtuple 命名元组
▸ itertools:无限迭代、排列组合、分组、串联,处理大规模数据不占用额外内存