mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
Fix-up moving average example.
This commit is contained in:
parent
bd16edd305
commit
d40285a986
1 changed files with 2 additions and 3 deletions
|
@ -455,10 +455,9 @@ added elements by appending to the right and popping to the left::
|
|||
# moving_average([40, 30, 50, 46, 39, 44]) --> 40.0 42.0 45.0 43.0
|
||||
# http://en.wikipedia.org/wiki/Moving_average
|
||||
it = iter(iterable)
|
||||
d = deque(itertools.islice(it, n))
|
||||
d = deque(itertools.islice(it, n-1))
|
||||
d.appendleft(0)
|
||||
s = sum(d)
|
||||
if len(d) == n:
|
||||
yield s / n
|
||||
for elem in it:
|
||||
s += elem - d.popleft()
|
||||
d.append(elem)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue