Last active
July 24, 2017 13:21
-
-
Save particledecay/d508a567b7334218b8b7ec1dd8b241c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def chop(sliceable, columns): | |
| """Return multiple selected slices of an iterable, in desired order. | |
| Args: | |
| sliceable (list): the original list to chop up | |
| columns (list): a list of integers (indices) or tuples (slices) | |
| Returns: | |
| (list) the final chopped up list | |
| Examples: | |
| >>> test_row = ['zero', 'one', 'two', 'three', 'four', 'five'] | |
| >>> chop(test_row, [0, 2, 4]) | |
| ['zero', 'two', 'four'] | |
| >>> chop(test_row, [(1, 3), 5]) | |
| ['one', 'two', 'five'] | |
| """ | |
| slices = map(lambda x: slice(*x) if isinstance(x, tuple) else slice(x, x + 1), columns) | |
| chopped = [sliceable[c] for c in slices] | |
| return list(itertools.chain.from_iterable(chopped)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment