Skip to content

Commit 97f43b5

Browse files
committed
Make itertools recipes into a single, sectioned code block
1 parent 548526b commit 97f43b5

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Doc/library/itertools.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,12 +838,17 @@ and :term:`generators <generator>` which incur interpreter overhead.
838838

839839
.. testcode::
840840

841+
from itertools import (accumulate, batched, chain, combinations, compress,
842+
count, cycle, filterfalse, groupby, islice, permutations, product,
843+
repeat, starmap, tee, zip_longest)
841844
from collections import Counter, deque
842845
from contextlib import suppress
843846
from functools import reduce
844847
from math import comb, prod, sumprod, isqrt
845848
from operator import is_not, itemgetter, getitem, mul, neg
846849

850+
# ==== Basic one liners ====
851+
847852
def take(n, iterable):
848853
"Return first n items of the iterable as a list."
849854
return list(islice(iterable, n))
@@ -908,6 +913,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
908913
# all_equal('4٤௪౪໔', key=int) → True
909914
return len(take(2, groupby(iterable, key))) <= 1
910915

916+
# ==== Data streams ====
917+
911918
def unique_justseen(iterable, key=None):
912919
"Yield unique elements, preserving order. Remember only the element just seen."
913920
# unique_justseen('AAAABBBCCDAABBB') → A B C D A B
@@ -1014,10 +1021,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
10141021
while True:
10151022
yield function()
10161023

1017-
1018-
The following recipes have a more mathematical flavor:
1019-
1020-
.. testcode::
1024+
# ==== Mathematical operations ====
10211025

10221026
def multinomial(*counts):
10231027
"Number of distinct arrangements of a multiset."
@@ -1036,6 +1040,8 @@ The following recipes have a more mathematical flavor:
10361040
# sum_of_squares([10, 20, 30]) → 1400
10371041
return sumprod(*tee(iterable))
10381042
1043+
# ==== Matrix operations ====
1044+
10391045
def reshape(matrix, columns):
10401046
"Reshape a 2-D matrix to have a given number of columns."
10411047
# reshape([(0, 1), (2, 3), (4, 5)], 3) → (0, 1, 2), (3, 4, 5)
@@ -1073,6 +1079,8 @@ The following recipes have a more mathematical flavor:
10731079
windowed_signal = sliding_window(padded_signal, n)
10741080
return map(sumprod, repeat(kernel), windowed_signal)
10751081

1082+
# ==== Polynomial arithmetic ====
1083+
10761084
def polynomial_from_roots(roots):
10771085
"""Compute a polynomial's coefficients from its roots.
10781086

@@ -1106,6 +1114,8 @@ The following recipes have a more mathematical flavor:
11061114
powers = reversed(range(1, n))
11071115
return list(map(mul, coefficients, powers))
11081116

1117+
# ==== Number theory ====
1118+
11091119
def sieve(n):
11101120
"Primes less than n."
11111121
# sieve(30) → 2 3 5 7 11 13 17 19 23 29

0 commit comments

Comments
 (0)