diff options
author | Raymond Hettinger <python@rcn.com> | 2009-05-22 01:06:44 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-05-22 01:06:44 (GMT) |
commit | 9b6f13ee82ec81a7e88e68094998631559e93f11 (patch) | |
tree | 070b8f859f92e6d10d37dba3116b88d6cf2f8667 | |
parent | a50af069318213a8e129e9fc02745446517797f0 (diff) | |
download | cpython-9b6f13ee82ec81a7e88e68094998631559e93f11.zip cpython-9b6f13ee82ec81a7e88e68094998631559e93f11.tar.gz cpython-9b6f13ee82ec81a7e88e68094998631559e93f11.tar.bz2 |
Fix-up moving average example.
-rw-r--r-- | Doc/library/collections.rst | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 5475776..3801cc8 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -476,16 +476,14 @@ added elements by appending to the right and popping to the left:: def moving_average(iterable, n=3): # moving_average([40, 30, 50, 46, 39, 44]) --> 40.0 42.0 45.0 43.0 # http://en.wikipedia.org/wiki/Moving_average - n = float(n) 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) - yield s / n + yield s / float(n) The :meth:`rotate` method provides a way to implement :class:`deque` slicing and deletion. For example, a pure python implementation of ``del d[n]`` relies on |