summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2020-12-30 20:51:19 (GMT)
committerGitHub <noreply@github.com>2020-12-30 20:51:19 (GMT)
commitf421bfce80730cb0ff5cbe14727ac30cf0462eed (patch)
tree5c6fd1941e97fd60f8b855bd27851e002b738103
parent4ac923f2756f835f512339ee181348cc535ab07f (diff)
downloadcpython-f421bfce80730cb0ff5cbe14727ac30cf0462eed.zip
cpython-f421bfce80730cb0ff5cbe14727ac30cf0462eed.tar.gz
cpython-f421bfce80730cb0ff5cbe14727ac30cf0462eed.tar.bz2
Minor improvements to the convolve() recipe (GH-24012)
* Minor improvement to speed and space efficiency for the convolve() recipe * Don't require convolve's kernel to be a sequence.
-rw-r--r--Doc/library/itertools.rst4
1 files changed, 2 insertions, 2 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 03416a0..fd77f99 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -791,9 +791,9 @@ which incur interpreter overhead.
# convolve(data, [0.25, 0.25, 0.25, 0.25]) --> Moving average (blur)
# convolve(data, [1, -1]) --> 1st finite difference (1st derivative)
# convolve(data, [1, -2, 1]) --> 2nd finite difference (2nd derivative)
- kernel = list(reversed(kernel))
+ kernel = tuple(kernel)[::-1]
n = len(kernel)
- window = collections.deque([0] * n, maxlen=n)
+ window = collections.deque([0], maxlen=n) * n
for x in chain(signal, repeat(0, n-1)):
window.append(x)
yield sum(map(operator.mul, kernel, window))