Python itertools 模块
itertools 提供高效迭代工具函数,避免创建中间列表,提升内存效率。
chain:连接迭代器
将多个迭代器连接为一个:
Python
from itertools import chain
# 连接多个列表
result = list(chain([1, 2], [3, 4], [5]))
print(result) # [1, 2, 3, 4, 5]
# 连接不同类型
result = list(chain('ABC', [1, 2], range(3)))
print(result) # ['A', 'B', 'C', 1, 2, 0, 1, 2]
# chain.from_iterable:从单个可迭代对象展开
nested = [[1, 2], [3, 4], [5]]
result = list(chain.from_iterable(nested))
print(result) # [1, 2, 3, 4, 5]
扁平化嵌套列表
Python
from itertools import chain
def flatten(matrix):
return chain.from_iterable(matrix)
matrix = [[1, 2, 3], [4, 5], [6]]
print(list(flatten(matrix))) # [1, 2, 3, 4, 5, 6]
zip_longest:不等长配对
不等长迭代器配对,用填充值补齐:
Python
from itertools import zip_longest
# 默认用 None 填充
result = list(zip_longest([1, 2, 3], ['a', 'b']))
print(result) # [(1, 'a'), (2, 'b'), (3, None)]
# 自定义填充值
result = list(zip_longest([1, 2, 3], ['a', 'b'], fillvalue='-'))
print(result) # [(1, 'a'), (2, 'b'), (3, '-')]
# 多个序列配对
result = list(zip_longest([1, 2], [10, 20, 30], [100], fillvalue=0))
print(result) # [(1, 10, 100), (2, 20, 0), (0, 30, 0)]
islice:迭代器切片
对迭代器进行切片,不创建完整列表:
Python
from itertools import islice
# 取前n个
result = list(islice(range(100), 5))
print(result) # [0, 1, 2, 3, 4]
# 指定起点和终点
result = list(islice(range(10), 2, 6))
print(result) # [2, 3, 4, 5]
# 指定步长
result = list(islice(range(20), 0, 10, 2))
print(result) # [0, 2, 4, 6, 8]
# 切片无限迭代器
from itertools import count
result = list(islice(count(10, 2), 5))
print(result) # [10, 12, 14, 16, 18]
starmap:映射解包
对每个元素解包后应用函数:
Python
from itertools import starmap
# 相当于 map(pow, [2, 3], [5, 2])
result = list(starmap(pow, [(2, 5), (3, 2)]))
print(result) # [32, 9]
# 多参数函数
data = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
result = list(starmap(lambda a, b, c: a + b + c, data))
print(result) # [6, 15, 24]
filterfalse:反向过滤
保留不满足条件的元素:
Python
from itertools import filterfalse
# 过滤偶数(保留奇数)
result = list(filterfalse(lambda x: x % 2 == 0, range(10)))
print(result) # [1, 3, 5, 7, 9]
# 过滤空值
data = ['', 'a', None, 'b', '', 'c']
result = list(filterfalse(lambda x: not x, data))
print(result) # ['a', 'b', 'c']
dropwhile/takewhile:条件跳过/取值
Python
from itertools import dropwhile, takewhile
# dropwhile:跳过直到条件不满足
data = [1, 3, 5, 7, 2, 4, 6]
result = list(dropwhile(lambda x: x < 5, data))
print(result) # [7, 2, 4, 6](跳过1,3,5)
# takewhile:取值直到条件不满足
result = list(takewhile(lambda x: x < 5, data))
print(result) # [1, 3](只取满足条件的连续元素)
accumulate:累积计算
累积应用二元函数:
Python
from itertools import accumulate
# 默认累积求和
result = list(accumulate([1, 2, 3, 4, 5]))
print(result) # [1, 3, 6, 10, 15]
# 累积乘积
result = list(accumulate([1, 2, 3, 4], initial=1, func=lambda a, b: a * b))
print(result) # [1, 1, 2, 6, 24]
# 累积最大值
result = list(accumulate([3, 1, 4, 1, 5], max))
print(result) # [3, 3, 4, 4, 5]
# 指定初始值
result = list(accumulate([1, 2, 3], initial=10))
print(result) # [10, 11, 13, 16]
product:笛卡尔积
生成多个序列的所有组合:
Python
from itertools import product
# 两序列组合
result = list(product(['A', 'B'], [1, 2]))
print(result) # [('A', 1), ('A', 2), ('B', 1), ('B', 2)]
# 单序列重复组合
result = list(product(['A', 'B'], repeat=2))
print(result) # [('A', 'A'), ('A', 'B'), ('B', 'A'), ('B', 'B')]
permutations:排列
生成指定长度的排列:
Python
from itertools import permutations
# 全排列
result = list(permutations(['A', 'B', 'C']))
print(result) # [('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ...]
# 指定长度
result = list(permutations(['A', 'B', 'C'], 2))
print(result) # [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
combinations:组合
生成指定长度的组合(无重复):
Python
from itertools import combinations
result = list(combinations(['A', 'B', 'C', 'D'], 2))
print(result) # [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]
combinations_with_replacement:允许重复的组合
Python
from itertools import combinations_with_replacement
result = list(combinations_with_replacement(['A', 'B'], 2))
print(result) # [('A', 'A'), ('A', 'B'), ('B', 'B')]
groupby:分组
按连续相同元素分组:
Python
from itertools import groupby
# 简单分组
data = [1, 1, 1, 2, 2, 3, 3, 3, 3]
for key, group in groupby(data):
print(key, list(group))
# 1 [1, 1, 1]
# 2 [2, 2]
# 3 [3, 3, 3, 3]
# 使用键函数分组
data = [('a', 1), ('a', 2), ('b', 3), ('b', 4)]
for key, group in groupby(data, key=lambda x: x[0]):
print(key, list(group))
# a [('a', 1), ('a', 2)]
# b [('b', 3), ('b', 4)]
# 需要先排序
data = ['apple', 'banana', 'apricot', 'blueberry', 'cherry']
sorted_data = sorted(data, key=lambda x: x[0])
for key, group in groupby(sorted_data, key=lambda x: x[0]):
print(key, list(group))
# a ['apple', 'apricot']
# b ['banana', 'blueberry']
# c ['cherry']
常用函数分类
| 类别 | 函数 |
|---|---|
| 连接 | chain, zip_longest |
| 切片 | islice |
| 过滤 | filterfalse, dropwhile, takewhile |
| 映射 | starmap, accumulate |
| 组合 | product, permutations, combinations |
| 分组 | groupby |
| 无限 | count, cycle, repeat |
要点总结
| 函数 | 用途 |
|---|---|
chain | 连接多个迭代器 |
zip_longest | 不等长配对 |
islice | 迭代器切片 |
filterfalse | 反向过滤 |
accumulate | 累积计算 |
groupby | 按键分组(需先排序) |
D:\git2\jwdev\articles\PYTHON\进阶\迭代器与生成器\itertools模块.md
📝 发现内容有误?点击此处直接编辑