summaryrefslogtreecommitdiffstats
path: root/Lib
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 /Lib
parentcc7c6801945c6a7373553b78bd899ce09681ec0a (diff)
downloadcpython-750368cbcd20393026f3bf695195f1a2cba490b5.zip
cpython-750368cbcd20393026f3bf695195f1a2cba490b5.tar.gz
cpython-750368cbcd20393026f3bf695195f1a2cba490b5.tar.bz2
Add more itertool recipes (GH-28165)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_itertools.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 0cf7eaf..aa81076 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -2394,6 +2394,23 @@ Samuele
... else:
... return starmap(func, repeat(args, times))
+>>> 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
+
+>>> import collections
+>>> def sliding_window(iterable, n):
+... # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
+... it = iter(iterable)
+... window = collections.deque(islice(it, n), maxlen=n)
+... if len(window) == n:
+... yield tuple(window)
+... for x in it:
+... window.append(x)
+... yield tuple(window)
+
>>> def grouper(n, iterable, fillvalue=None):
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
... args = [iter(iterable)] * n
@@ -2570,6 +2587,12 @@ True
>>> list(grouper(3, 'abcdefg', 'x'))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
+>>> list(triplewise('ABCDEFG'))
+[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
+
+>>> list(sliding_window('ABCDEFG', 4))
+[('A', 'B', 'C', 'D'), ('B', 'C', 'D', 'E'), ('C', 'D', 'E', 'F'), ('D', 'E', 'F', 'G')]
+
>>> list(roundrobin('abc', 'd', 'ef'))
['a', 'd', 'e', 'b', 'f', 'c']