Rolling or sliding window iterator?

后端 未结 23 2048
南方客
南方客 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:05

    I tested a few solutions and one I came up with and found the one I came up with to be the fastest so I thought I would share it.

    import itertools
    import sys
    
    def windowed(l, stride):
        return zip(*[itertools.islice(l, i, sys.maxsize) for i in range(stride)])
    

提交回复
热议问题