mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Add more itertool recipes (GH-28165)
(cherry picked from commit 750368cbcd)
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
This commit is contained in:
parent
ef70413777
commit
be33e58699
2 changed files with 43 additions and 2 deletions
|
|
@ -492,6 +492,8 @@ loops that truncate the stream.
|
||||||
next(b, None)
|
next(b, None)
|
||||||
return zip(a, b)
|
return zip(a, b)
|
||||||
|
|
||||||
|
.. versionadded:: 3.10
|
||||||
|
|
||||||
|
|
||||||
.. function:: permutations(iterable, r=None)
|
.. function:: permutations(iterable, r=None)
|
||||||
|
|
||||||
|
|
@ -812,11 +814,27 @@ which incur interpreter overhead.
|
||||||
return starmap(func, repeat(args, times))
|
return starmap(func, repeat(args, times))
|
||||||
|
|
||||||
def grouper(iterable, n, fillvalue=None):
|
def grouper(iterable, n, fillvalue=None):
|
||||||
"Collect data into fixed-length chunks or blocks"
|
"Collect data into non-overlapping fixed-length chunks or blocks"
|
||||||
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
|
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
|
||||||
args = [iter(iterable)] * n
|
args = [iter(iterable)] * n
|
||||||
return zip_longest(*args, fillvalue=fillvalue)
|
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):
|
def roundrobin(*iterables):
|
||||||
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
|
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
|
||||||
# Recipe credited to George Sakkis
|
# Recipe credited to George Sakkis
|
||||||
|
|
|
||||||
|
|
@ -2393,6 +2393,23 @@ def test_permutations_sizeof(self):
|
||||||
... else:
|
... else:
|
||||||
... return starmap(func, repeat(args, times))
|
... 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):
|
>>> def grouper(n, iterable, fillvalue=None):
|
||||||
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
|
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
|
||||||
... args = [iter(iterable)] * n
|
... args = [iter(iterable)] * n
|
||||||
|
|
@ -2569,6 +2586,12 @@ def test_permutations_sizeof(self):
|
||||||
>>> list(grouper(3, 'abcdefg', 'x'))
|
>>> list(grouper(3, 'abcdefg', 'x'))
|
||||||
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', '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'))
|
>>> list(roundrobin('abc', 'd', 'ef'))
|
||||||
['a', 'd', 'e', 'b', 'f', 'c']
|
['a', 'd', 'e', 'b', 'f', 'c']
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue