summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2021-09-07 17:29:00 (GMT)
committerGitHub <noreply@github.com>2021-09-07 17:29:00 (GMT)
commit750368cbcd20393026f3bf695195f1a2cba490b5 (patch)
tree25c217a88b942eed7ad9fbee097f8faf1236597c /Doc/library
parentcc7c6801945c6a7373553b78bd899ce09681ec0a (diff)
downloadcpython-750368cbcd20393026f3bf695195f1a2cba490b5.zip
cpython-750368cbcd20393026f3bf695195f1a2cba490b5.tar.gz
cpython-750368cbcd20393026f3bf695195f1a2cba490b5.tar.bz2
Add more itertool recipes (GH-28165)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/itertools.rst22
1 files changed, 20 insertions, 2 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 254e055..bf60a0c 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -492,6 +492,8 @@ loops that truncate the stream.
next(b, None)
return zip(a, b)
+ .. versionadded:: 3.10
+
.. function:: permutations(iterable, r=None)
@@ -812,11 +814,27 @@ which incur interpreter overhead.
return starmap(func, repeat(args, times))
def grouper(iterable, n, fillvalue=None):
- "Collect data into fixed-length chunks or blocks"
- # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
+ "Collect data into non-overlapping fixed-length chunks or blocks"
+ # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
+ def triplewise(iterable):
+ "Return overlapping triplets from an iterable"
+ # pairwise('ABCDEFG') -> ABC BCD CDE DEF EFG
+ for (a, _), (b, c) in pairwise(pairwise(iterable)):
+ yield a, b, c
+
+ def sliding_window(iterable, n):
+ # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
+ it = iter(iterable)
+ window = deque(islice(it, n), maxlen=n)
+ if len(window) == n:
+ yield tuple(window)
+ for x in it:
+ window.append(x)
+ yield tuple(window)
+
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis