mirror of
https://github.com/python/cpython.git
synced 2025-09-30 20:31:52 +00:00
[3.12] Use match/case in grouper() recipe (gh-113059) (gh-113197)
This commit is contained in:
parent
85ee49c434
commit
b9c5ffe6e7
1 changed files with 9 additions and 8 deletions
|
@ -914,14 +914,15 @@ which incur interpreter overhead.
|
|||
# grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
|
||||
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
|
||||
args = [iter(iterable)] * n
|
||||
if incomplete == 'fill':
|
||||
return zip_longest(*args, fillvalue=fillvalue)
|
||||
elif incomplete == 'strict':
|
||||
return zip(*args, strict=True)
|
||||
elif incomplete == 'ignore':
|
||||
return zip(*args)
|
||||
else:
|
||||
raise ValueError('Expected fill, strict, or ignore')
|
||||
match incomplete:
|
||||
case 'fill':
|
||||
return zip_longest(*args, fillvalue=fillvalue)
|
||||
case 'strict':
|
||||
return zip(*args, strict=True)
|
||||
case 'ignore':
|
||||
return zip(*args)
|
||||
case _:
|
||||
raise ValueError('Expected fill, strict, or ignore')
|
||||
|
||||
def sliding_window(iterable, n):
|
||||
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue