itertools
— 创建高效循环迭代器的函数
¶
此模块实现了许多 iterator 构建块受 APL、Haskell 和 SML 构造所启发。每个被重新铸造成适合 Python 的形式。
此模块标准化了一组快速、内存高效的核心工具,通过它们自身或将它们组合在一起很有用。它们一起形成了 "迭代器代数",使以纯 Python 简洁且高效地构造专用工具成为可能。
例如,SML 提供的制表工具:
tabulate(f)
产生一系列
f(0), f(1), ...
。Python 可以达成相同效果通过组合
map()
and
count()
以形成
map(f, count())
.
These tools and their built-in counterparts also work well with the high-speed functions in the
operator
module. For example, the multiplication operator can be mapped across two vectors to form an efficient dot-product:
sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))
.
无限迭代器:
|
迭代器 |
自变量 |
结果 |
范例 |
|---|---|---|---|
|
[start[, step]] |
start, start+step, start+2*step, … |
|
|
|
p |
p0, p1, … plast, p0, p1, … |
|
|
|
elem [,n] |
elem, elem, elem, … endlessly or up to n times |
|
Iterators terminating on the shortest input sequence:
|
迭代器 |
自变量 |
结果 |
范例 |
|---|---|---|---|
|
p [,func] |
p0, p0+p1, p0+p1+p2, … |
|
|
|
p, n |
(p0, p1, …, p_n-1), … |
|
|
|
p, q, … |
p0, p1, … plast, q0, q1, … |
|
|
|
iterable |
p0, p1, … plast, q0, q1, … |
|
|
|
data, selectors |
(d[0] if s[0]), (d[1] if s[1]), … |
|
|
|
predicate, seq |
seq[n], seq[n+1], starting when predicate fails |
|
|
|
predicate, seq |
elements of seq where predicate(elem) fails |
|
|
|
iterable[, key] |
sub-iterators grouped by value of key(v) |
|
|
|
seq, [start,] stop [, step] |
elements from seq[start:stop:step] |
|
|
|
iterable |
(p[0], p[1]), (p[1], p[2]) |
|
|
|
func, seq |
func(*seq[0]), func(*seq[1]), … |
|
|
|
predicate, seq |
seq[0], seq[1], until predicate fails |
|
|
|
it, n |
it1, it2, … itn splits one iterator into n |
||
|
p, q, … |
(p[0], q[0]), (p[1], q[1]), … |
|
组合迭代器:
|
迭代器 |
自变量 |
结果 |
|---|---|---|
|
p, q, … [repeat=1] |
cartesian product, equivalent to a nested for-loop |
|
|
p[, r] |
r-length tuples, all possible orderings, no repeated elements |
|
|
p, r |
r-length tuples, in sorted order, no repeated elements |
|
|
p, r |
r-length tuples, in sorted order, with repeated elements |
|
范例 |
结果 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Itertool Functions ¶
The following functions all construct and return iterators. Some provide streams of infinite length, so they should only be accessed by functions or loops that truncate the stream.
- itertools. accumulate ( iterable [ , function , * , initial=None ] ) ¶
-
Make an iterator that returns accumulated sums or accumulated results from other binary functions.
The function defaults to addition. The function should accept two arguments, an accumulated total and a value from the iterable .
若 initial value is provided, the accumulation will start with that value and the output will have one more element than the input iterable.
大致相当于:
def accumulate(iterable, function=operator.add, *, initial=None): 'Return running totals' # accumulate([1,2,3,4,5]) → 1 3 6 10 15 # accumulate([1,2,3,4,5], initial=100) → 100 101 103 106 110 115 # accumulate([1,2,3,4,5], operator.mul) → 1 2 6 24 120 iterator = iter(iterable) total = initial if initial is None: try: total = next(iterator) except StopIteration: return yield total for element in iterator: total = function(total, element) yield total