enter code here
"""Write a function that takes a list of numbers and returns the cumulative sum; that is, a new list where the ith element is the sum of the first i + 1 elements from the original list. For example, the cumulative sum of [1, 2, 3] is [1, 3, 6]."""
def list(l):
new_l = []
j = 0
for i in l:
for i in range(l.index(i)+1):
j += l[i]
new_l.append(j) # this for loop seems to accumulate twice
return new_l
print list([1,2,3,4]) # [1,4,10,20] other than [1,3,4,10]
就是这样。感谢您通过打印[1,3,4,10]使其工作的答案!请帮我解决一下我的Python代码基础知识:'for'循环列表和累积和
我想你是指'[1,3,6,10]'。 – 2013-05-01 09:41:48