Rolling or sliding window iterator?

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

    There is a library which does exactly what you need:

    import more_itertools
    list(more_itertools.windowed([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],n=3, step=3))
    
    Out: [(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12), (13, 14, 15)]
    

提交回复
热议问题