Last active
November 30, 2019 22:30
-
-
Save james-mchugh/3be2cfd0cfc1360c19544c763998314e to your computer and use it in GitHub Desktop.
Get start and end indices of repetitive sequences from a list.
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 get_ones_coords(lst, repeat_value = 1): | |
indices = range(len(lst)) | |
for key_, group in groupby(indices, key=lambda i: lst[i]): | |
if key_ == repeat_value: | |
group = list(group) | |
yield group[0], group[-1] + 1 | |
lst = [1, 1, 0, 0, 0, 1, 1, 1, 0, 1] | |
print(list(get_ones_coords(lst))) # [(0, 2), (5, 8), (9, 10)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment