I have a list of tuples. First element is index. I need to delete tuples which has subsequent indexes only leaving first tuple from each sequence:
In:
[
[0, 100],
[1, 200],
[5, 600],
[6, 300],
[7, 800],
[9, 300],
[11, 100],
[14, 300],
]
Out:
[
[0, 100],
[5, 600],
[9, 300],
[11, 100],
[14, 300],
]
>Solution :
Using a simple loop (and assuming l the input list):
prev = -float('inf')
out = []
for x in l:
if x[0]>prev+1:
out.append(x)
prev=x[0]
output: [[0, 100], [5, 600], [9, 300], [11, 100], [14, 300]]