diff options
author | Raymond Hettinger <python@rcn.com> | 2010-12-01 10:49:19 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-12-01 10:49:19 (GMT) |
commit | 5ce0aa236f5417a5be6fba80c6aaa38ed456ef59 (patch) | |
tree | 4fa0c0c69af88249497070c31c0c55da3daf50ff /Doc/library/itertools.rst | |
parent | c79fb0e52d297e0599a37be2652e75e3abc35690 (diff) | |
download | cpython-5ce0aa236f5417a5be6fba80c6aaa38ed456ef59.zip cpython-5ce0aa236f5417a5be6fba80c6aaa38ed456ef59.tar.gz cpython-5ce0aa236f5417a5be6fba80c6aaa38ed456ef59.tar.bz2 |
Add recipe to itertools doc.
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r-- | Doc/library/itertools.rst | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index f612a1c..bab1680 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -653,6 +653,14 @@ which incur interpreter overhead. pending -= 1 nexts = cycle(islice(nexts, pending)) + def accumulate(iterable): + 'Emit a running total' + # accumulate([1,2,3,4,5]) --> 1 3 6 10 15 + total = 0 + for element in iterable: + total += element + yield total + def partition(pred, iterable): 'Use a predicate to partition entries into false entries and true entries' # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9 |