mirror of
https://github.com/python/cpython.git
synced 2025-10-03 13:45:29 +00:00
[3.11] Sync the batched() recipe with the 3.12 implementation (GH-98446)
This commit is contained in:
parent
30b9c4d784
commit
07cc997e00
1 changed files with 2 additions and 6 deletions
|
@ -887,6 +887,8 @@ which incur interpreter overhead.
|
|||
def batched(iterable, n):
|
||||
"Batch data into lists of length n. The last batch may be shorter."
|
||||
# batched('ABCDEFG', 3) --> ABC DEF G
|
||||
if n < 1:
|
||||
raise ValueError('n must be at least one')
|
||||
it = iter(iterable)
|
||||
while (batch := list(islice(it, n))):
|
||||
yield batch
|
||||
|
@ -1272,12 +1274,6 @@ which incur interpreter overhead.
|
|||
[['A', 'B'], ['C', 'D'], ['E', 'F'], ['G']]
|
||||
>>> list(batched('ABCDEFG', 1))
|
||||
[['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']]
|
||||
>>> list(batched('ABCDEFG', 0))
|
||||
[]
|
||||
>>> list(batched('ABCDEFG', -1))
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.
|
||||
>>> s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
>>> all(list(flatten(batched(s[:n], 5))) == list(s[:n]) for n in range(len(s)))
|
||||
True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue