mirror of
https://github.com/python/cpython.git
synced 2025-11-12 07:02:33 +00:00
Add recipe to itertools doc.
This commit is contained in:
parent
c79fb0e52d
commit
5ce0aa236f
1 changed files with 8 additions and 0 deletions
|
|
@ -653,6 +653,14 @@ which incur interpreter overhead.
|
||||||
pending -= 1
|
pending -= 1
|
||||||
nexts = cycle(islice(nexts, pending))
|
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):
|
def partition(pred, iterable):
|
||||||
'Use a predicate to partition entries into false entries and true entries'
|
'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
|
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue