尽管检查但索引超出范围

    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 access t2 list that is empty. To append an element to a list you should use <list>.append(<element>), that is t2.append(total) in your case.

点赞
评论