t = [1, 2, 3]
def cumsum(t):
t2 = []
total = 0
i = 0
while i < len(t):
total += t[i]
t2[i].append(total)
i += 1
return t2
cumsum(t)
此代码采用前两个列表整数的和,并将其附加到另一个列表。 我觉得这应该在逻辑上可行,并且我不明白为什么当len(t)= 3时,如果我
t = [1, 2, 3]
def cumsum(t):
t2 = []
total = 0
i = 0
while i < len(t):
total += t[i]
t2[i].append(total)
i += 1
return t2
cumsum(t)
此代码采用前两个列表整数的和,并将其附加到另一个列表。 我觉得这应该在逻辑上可行,并且我不明白为什么当len(t)= 3时,如果我
Because you are using index
i
to accesst2
list that is empty. To append an element to a list you should use<list>.append(<element>)
, that ist2.append(total)
in your case.