Skip to content

Instantly share code, notes, and snippets.

@james-mchugh
james-mchugh / get_sequences.py
Last active November 30, 2019 22:30
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)]