mirror of
https://github.com/python/cpython.git
synced 2025-10-03 21:55:41 +00:00
Use match/case in grouper() recipe (gh-113059)
Use match/case in grouper() reciper
This commit is contained in:
parent
480b4b359d
commit
2111795d0c
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='strict') --> ABC DEF ValueError
|
||||||
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
|
# grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
|
||||||
args = [iter(iterable)] * n
|
args = [iter(iterable)] * n
|
||||||
if incomplete == 'fill':
|
match incomplete:
|
||||||
return zip_longest(*args, fillvalue=fillvalue)
|
case 'fill':
|
||||||
elif incomplete == 'strict':
|
return zip_longest(*args, fillvalue=fillvalue)
|
||||||
return zip(*args, strict=True)
|
case 'strict':
|
||||||
elif incomplete == 'ignore':
|
return zip(*args, strict=True)
|
||||||
return zip(*args)
|
case 'ignore':
|
||||||
else:
|
return zip(*args)
|
||||||
raise ValueError('Expected fill, strict, or ignore')
|
case _:
|
||||||
|
raise ValueError('Expected fill, strict, or ignore')
|
||||||
|
|
||||||
def sliding_window(iterable, n):
|
def sliding_window(iterable, n):
|
||||||
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
|
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue