在迭代中獲得累積的數字總和

Python 3.x >= 3.2

accumulate 產生數字的累積和(或乘積)。

>>> import itertools as it
>>> import operator

>>> list(it.accumulate([1,2,3,4,5]))
[1, 3, 6, 10, 15]

>>> list(it.accumulate([1,2,3,4,5], func=operator.mul)) 
[1, 2, 6, 24, 120]