Q:

Generating overlapping sequences

I have a dataset with the following information. The timebin variable is an identifier for the time period of the data. It can be assumed that timebin is unique and without any gaps (i.e. the data will always contain 2 if it contains 1 and 3).
timebin,lat,lon
0,9.0,2.0
1,12.0,4.0
2,15.0,6.0
3,18.0,8.0
4,21.0,10.0
5,24.0,12.0
6,27.0,14.0
7,30.0,16.0

I want to generate all the sequences of a fixed-length l with an amount of overlap o. For instance, for l=4 and o=2 the following groups of indices would be output:
[[0,1,2,3], [2,3,4,5], [4,5,6,7]]

This could be done using a loop, but I wonder if there is a more elegant and efficient way of doing it in python?

A:

Use list comprehension:
l = 4
o = 2
e = 7

print([[x for x in range(s, s + l)] for s in range(0, e, o) if s + l <= e + 1])

Result:
[[0, 1, 2, 3], [2, 3, 4, 5], [4, 5, 6, 7]]

