summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-05-22 01:11:26 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-05-22 01:11:26 (GMT)
commitd40285a986990d45c29e83943dc7783a4e976da5 (patch)
treee45d76f3fccbaf7d035faa9b42a403e2afde2973 /Doc/library
parentbd16edd305ba8067615e703a2cfeeb33907f40b8 (diff)
downloadcpython-d40285a986990d45c29e83943dc7783a4e976da5.zip
cpython-d40285a986990d45c29e83943dc7783a4e976da5.tar.gz
cpython-d40285a986990d45c29e83943dc7783a4e976da5.tar.bz2
Fix-up moving average example.
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/collections.rst5
1 files changed, 2 insertions, 3 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index 3f57348..4c64d61 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -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)