Created
May 7, 2021 15:00
-
-
Save CodeMan99/c9ca87dbec613b19f5cc730791eca754 to your computer and use it in GitHub Desktop.
Create left & right slices of a collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from operator import itemgetter | |
def create_slicer(*indexes): | |
""" | |
>>> l = list(range(10)) | |
>>> create_slicer(5)(l) | |
([0, 1, 2, 3, 4], [5, 6, 7, 8, 9]) | |
>>> create_slicer(3, 7)(l) | |
([0, 1, 2], [3, 4, 5, 6], [7, 8, 9]) | |
>>> create_slicer(3, 5, 7)(l) | |
([0, 1, 2], [3, 4], [5, 6], [7, 8, 9]) | |
""" | |
left = (None,) + indexes | |
right = indexes + (None,) | |
return itemgetter(*(slice(l, r) for l, r in zip(left, right))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment