-
-
Save Peilonrayz/e897757c722ef3682bdc51777a403dfe to your computer and use it in GitHub Desktop.
Split
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
def split(iterable, sep): | |
indexes = itertools.chain([0], itertools.repeat(1)) | |
for i, (key, group) in zip(indexes, itertools.groupby(iterable, sep.__ne__)): | |
if key: | |
carry = [] | |
yield itertools.chain(group, carry) | |
carry.extend(group) | |
else: | |
for _ in itertools.islice(group, i, None): | |
yield iter([]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment