Elegant pythonic cumsum

前端 未结 10 1106
-上瘾入骨i
-上瘾入骨i 2020-12-16 11:54

What would be an elegant and pythonic way to implement cumsum?
Alternatively - if there\'a already a built-in way to do it, that would be even better of course...

10条回答
  •  [愿得一人]
    2020-12-16 12:25

    in place:

    a=[1,2,3,4,5]
    def cumsum(a):
        for i in range(1,len(a)):
            a[i]+=a[i-1]
    
    cumsum(a)
    print a
    "[1, 3, 6, 10, 15]"
    

提交回复
热议问题