【发布时间】:2021-07-12 21:08:09
【问题描述】:
我正在尝试使用 for 循环将项目附加到列表列表中,但遇到 IndexError: list index out of range 错误。
class Solution:
counter = [[]]
def test(self):
i = 0
while i < 5:
Solution.counter[i].append(i)
i += 1
print(Solution.counter)
sol = Solution()
sol.test()
【问题讨论】:
-
你没有 counter[1] 。它不应该是 counter[i] ,只是把 counter[0].append(i)
-
这是一个列表列表,我想每次都将项目附加到不同的索引中。