Creates a list of partial sums.
- Use
itertools.accumulate()
to create the accumulated sum for each element. - Use
list()
to convert the result into a list.
代码实现
from itertools import accumulate
def cumsum(lst):
return list(accumulate(lst))
使用样例
cumsum(range(0, 15, 3)) # [0, 3, 9, 18, 30]