Rolling or sliding window iterator?

后端 未结 23 1393
南方客
南方客 2020-11-21 05:23

I need a rolling window (aka sliding window) iterable over a sequence/iterator/generator. Default Python iteration can be considered a special case, where the window length

23条回答
  •  温柔的废话
    2020-11-21 06:04

    here is a one liner. I timed it and it's comprable to the performance of the top answer and gets progressively better with larger seq from 20% slower with len(seq) = 20 and 7% slower with len(seq) = 10000

    zip(*[seq[i:(len(seq) - n - 1 + i)] for i in range(n)])
    

提交回复
热议问题