@@ -46,7 +46,7 @@ Iterator Arguments Results
4646==================== ============================ ================================================= =============================================================
4747Iterator Arguments Results Example
4848==================== ============================ ================================================= =============================================================
49- :func: `accumulate ` p p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) --> 1 3 6 10 15 ``
49+ :func: `accumulate ` p [,func] p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) --> 1 3 6 10 15 ``
5050:func: `chain ` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F ``
5151:func: `compress ` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F ``
5252:func: `dropwhile ` pred, seq seq[n], seq[n+1], starting when pred fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 ``
@@ -84,23 +84,46 @@ The following module functions all construct and return iterators. Some provide
8484streams of infinite length, so they should only be accessed by functions or
8585loops that truncate the stream.
8686
87- .. function :: accumulate(iterable)
87+ .. function :: accumulate(iterable[, func] )
8888
8989 Make an iterator that returns accumulated sums. Elements may be any addable
90- type including :class: `Decimal ` or :class: `Fraction `. Equivalent to::
90+ type including :class: `Decimal ` or :class: `Fraction `. If the optional
91+ *func * argument is supplied, it should be a function of two arguments
92+ and it will be used instead of addition.
9193
92- def accumulate(iterable):
94+ Equivalent to::
95+
96+ def accumulate(iterable, func=operator.add):
9397 'Return running totals'
9498 # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
99+ # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
95100 it = iter(iterable)
96101 total = next(it)
97102 yield total
98103 for element in it:
99- total = total + element
104+ total = func( total, element)
100105 yield total
101106
107+ Uses for the *func * argument include :func: `min ` for a running minimum,
108+ :func: `max ` for a running maximum, and :func: `operator.mul ` for a running
109+ product::
110+
111+ >>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
112+ >>> list(accumulate(data, operator.mul)) # running product
113+ [3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
114+ >>> list(accumulate(data, max)) # running maximum
115+ [3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
116+
117+ # Amortize a 5% loan of 1000 with 4 annual payments of 90
118+ >>> cashflows = [1000, -90, -90, -90, -90]
119+ >>> list(accumulate(cashflows, lambda bal, pmt: bal*1.05 + pmt))
120+ [1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]
121+
102122 .. versionadded :: 3.2
103123
124+ .. versionchanged :: 3.3
125+ Added the optional *func * parameter.
126+
104127.. function :: chain(*iterables)
105128
106129 Make an iterator that returns elements from the first iterable until it is
0 commit comments