How to effectively slice an array into overlapping subarrays, so that for
>>> N = 5
>>> L = 2 # could be any, less than N
>>> x = range(N)
the expected result is
[[1,0],[2,1],[3,2],[4,3]]
Here what I've tried:
>>> [ x[i:i-L:-1] for i in range(L-1,len(x)) ]
[[], [3, 2], [4, 3], [5, 4]] # wrong
>>> [ x[i:i-L:-1] for i in range(L,len(x)) ]
[[2, 1], [3, 2], [4, 3]] # wrong
>>> [ x[i:i-L if i-L >= 0 else None:-1] for i in range(L-1,len(x)) ]
[[1, 0], [2, 1], [3, 2], [4, 3]] # correct
It produces the desired result, but is it the best way to achieve it?
Are there some numpy, itertools functions that may help?