@@ -58,7 +58,7 @@ Iterator Arguments Results
58
58
:func: `compress ` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) → A C E F ``
59
59
:func: `dropwhile ` predicate, seq seq[n], seq[n+1], starting when predicate fails ``dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8 ``
60
60
:func: `filterfalse ` predicate, seq elements of seq where predicate(elem) fails ``filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8 ``
61
- :func: `groupby ` iterable[, key] sub-iterators grouped by value of key(v)
61
+ :func: `groupby ` iterable[, key] sub-iterators grouped by value of key(v) `` groupby(['A','B','DEF'], len) → (1, A B) (3, DEF) ``
62
62
:func: `islice ` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) → C D E F G ``
63
63
:func: `pairwise ` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') → AB BC CD DE EF FG ``
64
64
:func: `starmap ` func, seq func(\* seq[0]), func(\* seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000 ``
@@ -93,7 +93,7 @@ Examples Results
93
93
Itertool Functions
94
94
------------------
95
95
96
- The following module functions all construct and return iterators. Some provide
96
+ The following functions all construct and return iterators. Some provide
97
97
streams of infinite length, so they should only be accessed by functions or
98
98
loops that truncate the stream.
99
99
@@ -131,11 +131,12 @@ loops that truncate the stream.
131
131
total = function(total, element)
132
132
yield total
133
133
134
- The *function * argument can be set to :func: `min ` for a running
135
- minimum, :func: `max ` for a running maximum, or :func: `operator.mul `
136
- for a running product. `Amortization tables
137
- <https://www.ramseysolutions.com/real-estate/amortization-schedule> `_
138
- can be built by accumulating interest and applying payments:
134
+ To compute a running minimum, set *function * to :func: `min `.
135
+ For a running maximum, set *function * to :func: `max `.
136
+ Or for a running product, set *function * to :func: `operator.mul `.
137
+ To build an `Amortization table
138
+ <https://www.ramseysolutions.com/real-estate/amortization-schedule> `_,
139
+ accumulate the interest and apply payments:
139
140
140
141
.. doctest ::
141
142
@@ -202,10 +203,10 @@ loops that truncate the stream.
202
203
203
204
.. function :: chain(*iterables)
204
205
205
- Make an iterator that returns elements from the first iterable until it is
206
- exhausted, then proceeds to the next iterable, until all of the iterables are
207
- exhausted. Used for treating consecutive sequences as a single sequence.
208
- Roughly equivalent to::
206
+ Make an iterator that returns elements from the first iterable until
207
+ it is exhausted, then proceeds to the next iterable, until all of the
208
+ iterables are exhausted. This combines multiple data sources into a
209
+ single iterator. Roughly equivalent to::
209
210
210
211
def chain(*iterables):
211
212
# chain('ABC', 'DEF') → A B C D E F
@@ -353,10 +354,12 @@ loops that truncate the stream.
353
354
354
355
def cycle(iterable):
355
356
# cycle('ABCD') → A B C D A B C D A B C D ...
357
+
356
358
saved = []
357
359
for element in iterable:
358
360
yield element
359
361
saved.append(element)
362
+
360
363
while saved:
361
364
for element in saved:
362
365
yield element
@@ -396,8 +399,10 @@ loops that truncate the stream.
396
399
397
400
def filterfalse(predicate, iterable):
398
401
# filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8
402
+
399
403
if predicate is None:
400
404
predicate = bool
405
+
401
406
for x in iterable:
402
407
if not predicate(x):
403
408
yield x
@@ -474,7 +479,7 @@ loops that truncate the stream.
474
479
If *start * is zero or ``None ``, iteration starts at zero. Otherwise,
475
480
elements from the iterable are skipped until *start * is reached.
476
481
477
- If *stop * is ``None ``, iteration continues until the iterable is
482
+ If *stop * is ``None ``, iteration continues until the input is
478
483
exhausted, if at all. Otherwise, it stops at the specified position.
479
484
480
485
If *step * is ``None ``, the step defaults to one. Elements are returned
@@ -520,8 +525,10 @@ loops that truncate the stream.
520
525
521
526
def pairwise(iterable):
522
527
# pairwise('ABCDEFG') → AB BC CD DE EF FG
528
+
523
529
iterator = iter(iterable)
524
530
a = next(iterator, None)
531
+
525
532
for b in iterator:
526
533
yield a, b
527
534
a = b
@@ -584,7 +591,8 @@ loops that truncate the stream.
584
591
585
592
.. function :: product(*iterables, repeat=1)
586
593
587
- Cartesian product of input iterables.
594
+ `Cartesian product <https://en.wikipedia.org/wiki/Cartesian_product >`_
595
+ of the input iterables.
588
596
589
597
Roughly equivalent to nested for-loops in a generator expression. For example,
590
598
``product(A, B) `` returns the same as ``((x,y) for x in A for y in B) ``.
0 commit comments