| 123456789101112131415161718192021 |
- # Copyright PSF
- """
- Python 2 implementation of the accumulate function in itertools
- From the Python documentation https://docs.python.org/3/library/itertools.html#itertool-functions
- """
- import operator
- def accumulate(iterable, func=operator.add):
- 'Return running totals'
- # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
- # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
- it = iter(iterable)
- try:
- total = next(it)
- except StopIteration:
- return
- yield total
- for element in it:
- total = func(total, element)
- yield total
|