loops - Python range with start larger than stop -
i want extract i
indices of vector uniformly t
times. instance, if have vector x = [1,2,3,4,5,6,7]
, i = 3
, t = 5
, indices in each time must be:
t = 1; [1,2,3] t = 2; [4,5,6] t = 3; [7,1,2] t = 4; [3,4,5] t = 5; [6,7,1]
would possible in python range()
?
you can use itertools.islice
on itertools.cycle
. make cycle
object iterable, , slice
object using window size i
:
from itertools import cycle itertools import islice l = [1,2,3,4,5,6,7] t = 5; = 3 c = cycle(l) r = [list(islice(c, i)) _ in range(t)] # range appears here # [[1, 2, 3], [4, 5, 6], [7, 1, 2], [3, 4, 5], [6, 7, 1]]
you can apply different non-negative values of i
, , when i
greater length of list:
i = 10 r = [list(islice(c, i)) _ in range(t)] print(r) # [[1, 2, 3, 4, 5, 6, 7, 1, 2, 3], [4, 5, 6, 7, 1, 2, 3, 4, 5, 6], [7, 1, 2, 3, 4, 5, 6, 7, 1, 2], [3, 4, 5, 6, 7, 1, 2, 3, 4, 5], [6, 7, 1, 2, 3, 4, 5, 6, 7, 1]]
Comments
Post a Comment