I have an ordered list, for example:
my_list = [0.1, 0.2, 0.3, 0.4, 2.6, 2.7, 2.8, 2.9,
5.1, 5.2, 6.1, 6.2, 6.3, 7.1, 7.2, 7.3,
7.4, 7.5, 10.1, 10.2, 10.3, 10.4, 10.5]
I need intervals of numbers < 1s apart, where there are at least 3 numbers. I only want the start and end of the intervals. For example:
Output: [[0.1, 0.4], [2.6, 2.9], [5.1, 7.5], [10.1, 10.5]]
or
In[0]: print(start)
Output: [0.1, 2.6, 5.1, 10.1]
In[1]: print(end)
Output: [0.4, 2.9, 7.5, 10.5]
I've tried a variety of loops, but I'm having trouble getting only the start times appended to a new list, and also having trouble avoiding "Index out of range" when getting to the end of the list. Here is where I'm at currently:
for i in range(0, (len(my_list)-2)):
second = i + 1
third = i + 2
if ((my_list[third] - my_list[second])) < 1 and ((my_list[second] - my_list[i]) < 1):
temp.append(my_list[i])
else:
end.append(my_list[second])
start.append(temp[0])
temp.clear()
My solution to get only the start of the interval is to append the items to a temporary list, append the first and last element and clear that list. I'm sure there is a more elegant way to do this, and the list can be thousands of rows so I don't think this is a very efficient method.
Any help would be much appreciated.