Elegant pythonic cumsum

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

    for loops are pythonic

    def cumsum(vec):
        r = [vec[0]]
        for val in vec[1:]:
            r.append(r[-1] + val)
        return r
    

提交回复
热议问题