CodyKochmann
9/7/2016 - 2:07 PM

Calculate the sum of a window in Python iterable

Calculate the sum of a window in Python iterable

from collections import deque

def sum_of_window(iterable,window_size=3):
    # by: codykochmann
    window=deque()
    for i in iterable:
        window.append(i)
        if len(window) is window_size:
            yield sum(window)
            window.popleft()

for i in sum_of_window(range(10),3):
    print(i)