Generators for Engineers

A gentle introduction to infinite sequences.

Comments are moderated. It may take a few minutes before your comment appears.
Markdown is supported in your comments.

from collections import deque

def moving_average(iterator, length):
    d = deque(islice(iterator, 0, length))
    s = sum(d)
    yield s / length
    for i in iterator:
        s -= d.popleft()
        s += i
        d.append(i)
        yield s / length
Name:
Mail: (not shown)

Please type this: