How to find the cumulative sum of numbers in a list?

前端 未结 21 1621
挽巷
挽巷 2020-11-22 02:09
time_interval = [4, 6, 12]

I want to sum up the numbers like [4, 4+6, 4+6+12] in order to get the list t = [4, 10, 22].

21条回答
  •  时光说笑
    2020-11-22 02:38

    Behold:

    a = [4, 6, 12]
    reduce(lambda c, x: c + [c[-1] + x], a, [0])[1:]
    

    Will output (as expected):

    [4, 10, 22]
    

提交回复
热议问题