Rolling or sliding window iterator?

后端 未结 23 1838
南方客
南方客 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条回答
  •  萌比男神i
    2020-11-21 05:43

    def rolling_window(list, degree):
        for i in range(len(list)-degree+1):
            yield [list[i+o] for o in range(degree)]
    

    Made this for a rolling average function

提交回复
热议问题