itertools 是 Python 标准库的迭代器工具集,提供高效处理迭代对象的函数。
无限迭代器
| 函数 | 说明 |
|---|---|
count(start, step) | 无限计数器 |
cycle(iterable) | 无限循环迭代 |
repeat(elem, n) | 重复元素 n 次 |
| |
排列组合
| 函数 | 说明 |
|---|---|
product(*iterables) | 笛卡尔积 |
permutations(iterable, r) | 排列 |
combinations(iterable, r) | 组合(无重复) |
combinations_with_replacement | 组合(可重复) |
| |
筛选与分组
| 函数 | 说明 |
|---|---|
filterfalse(pred, iter) | 过滤不满足条件的元素 |
takewhile(pred, iter) | 持续取值直到条件为假 |
dropwhile(pred, iter) | 跳过直到条件为假 |
groupby(iterable, key) | 分组 |
| |
其他常用
| 函数 | 说明 |
|---|---|
chain(*iterables) | 连接多个迭代器 |
islice(iterable, stop) | 切片 |
zip_longest(*iterables) | 最长 zip |
| |
张芷铭的个人博客
Comments