Elegant pythonic cumsum

前端 未结 10 1110
-上瘾入骨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:33

    def cumsum(a):
         return map(lambda x: sum( a[0:x+1] ), range( 0, len(a) ))
    
    cumsum([1,2,3])
    
    > [1, 3, 6]
    

提交回复
热议问题