Skip to content

Instantly share code, notes, and snippets.

@james-mchugh
Last active November 30, 2019 22:30
Show Gist options
  • Save james-mchugh/3be2cfd0cfc1360c19544c763998314e to your computer and use it in GitHub Desktop.
Save james-mchugh/3be2cfd0cfc1360c19544c763998314e to your computer and use it in GitHub Desktop.
Get start and end indices of repetitive sequences from a list.
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