Find all consecutive sub-sequences of length n in a sequence

前端 未结 4 1284
野的像风
野的像风 2020-12-10 18:03

I want to find all consecutive sub-sequences of length n in a sequence.

E.g. say n was 3 and the sequence was:

[0,1,7,3,4,5,10]

I w

4条回答
  •  借酒劲吻你
    2020-12-10 18:42

    >>> x = [0,1,7,3,4,5,10]
    >>> [x[n:n+3] for n in range(len(x)-2)]
    [[0, 1, 7], [1, 7, 3], [7, 3, 4], [3, 4, 5], [4, 5, 10]]
    

提交回复
热议问题