From: Raymond Hettinger Date: Thu, 21 Apr 2011 18:09:28 +0000 (-0700) Subject: Add another example for accumulate(). X-Git-Tag: v3.3.0a1~2527 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=295c1d4f195bd7f7004bc9c4be59ec6925c800c0;p=python Add another example for accumulate(). --- diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 07378d1da5..9cdad6ee85 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -104,9 +104,13 @@ loops that truncate the stream. total = func(total, element) yield total - Uses for the *func* argument include :func:`min` for a running minimum, - :func:`max` for a running maximum, and :func:`operator.mul` for a running - product:: + There are a number of uses for the *func* argument. It can be set to + :func:`min` for a running minimum, :func:`max` for a running maximum, or + :func:`operator.mul` for a running product. Amortization tables can be + built by accumulating interest and applying payments. First-order + `recurrence relations `_ + can be modeled by supplying the initial value in the iterable and using only + the accumulated total in *func* argument:: >>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8] >>> list(accumulate(data, operator.mul)) # running product @@ -119,6 +123,17 @@ loops that truncate the stream. >>> list(accumulate(cashflows, lambda bal, pmt: bal*1.05 + pmt)) [1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001] + # Chaotic recurrence relation http://en.wikipedia.org/wiki/Logistic_map + >>> logistic_map = lambda x, _: r * x * (1 - x) + >>> r = 3.8 + >>> x0 = 0.4 + >>> inputs = repeat(x0, 36) # only the initial value is used + >>> [format(x, '.2f') for x in accumulate(inputs, logistic_map)] + ['0.40', '0.91', '0.30', '0.81', '0.60', '0.92', '0.29', '0.79', '0.63', + '0.88' ,'0.39', '0.90', '0.33', '0.84', '0.52', '0.95', '0.18', '0.57', + '0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32', + '0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60'] + .. versionadded:: 3.2 .. versionchanged:: 3.3