https://github.com/python/cpython/commit/9a57c932ecbe1004e5c5f40462df244669c7f1e6
commit: 9a57c932ecbe1004e5c5f40462df244669c7f1e6
branch: 3.13
author: Stan Ulbrych <[email protected]>
committer: rhettinger <[email protected]>
date: 2026-02-12T00:24:00-06:00
summary:
[3.13] Itertools recipes: Replace the tabulate() example with running_mean()
(gh-144483) (gh-144722)
files:
M Doc/library/itertools.rst
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 51bbca2cf0f2d4..8c352c939e9e54 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -842,7 +842,7 @@ and :term:`generators <generator>` which incur interpreter
overhead.
from contextlib import suppress
from functools import reduce
from math import comb, prod, sumprod, isqrt
- from operator import itemgetter, getitem, mul, neg
+ from operator import itemgetter, getitem, mul, neg, truediv
def take(n, iterable):
"Return first n items of the iterable as a list."
@@ -853,9 +853,10 @@ and :term:`generators <generator>` which incur interpreter
overhead.
# prepend(1, [2, 3, 4]) → 1 2 3 4
return chain([value], iterable)
- def tabulate(function, start=0):
- "Return function(0), function(1), ..."
- return map(function, count(start))
+ def running_mean(iterable):
+ "Yield the average of all values seen so far."
+ # running_mean([8.5, 9.5, 7.5, 6.5]) -> 8.5 9.0 8.5 8.0
+ return map(truediv, accumulate(iterable), count(1))
def repeatfunc(function, times=None, *args):
"Repeat calls to a function with specified arguments."
@@ -1210,8 +1211,8 @@ The following recipes have a more mathematical flavor:
[(0, 'a'), (1, 'b'), (2, 'c')]
- >>> list(islice(tabulate(lambda x: 2*x), 4))
- [0, 2, 4, 6]
+ >>> list(running_mean([8.5, 9.5, 7.5, 6.5]))
+ [8.5, 9.0, 8.5, 8.0]
>>> for _ in loops(5):
@@ -1748,6 +1749,10 @@ The following recipes have a more mathematical flavor:
# Old recipes and their tests which are guaranteed to continue to work.
+ def tabulate(function, start=0):
+ "Return function(0), function(1), ..."
+ return map(function, count(start))
+
def old_sumprod_recipe(vec1, vec2):
"Compute a sum of products."
return sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))
@@ -1827,6 +1832,10 @@ The following recipes have a more mathematical flavor:
.. doctest::
:hide:
+ >>> list(islice(tabulate(lambda x: 2*x), 4))
+ [0, 2, 4, 6]
+
+
>>> dotproduct([1,2,3], [4,5,6])
32
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]