我试图删除for
循环中的每个类实例。但是,列表长度满足for
循环迭代。见:
for n in range(len(myList)):
print(len(myList), n)
del myList[n]
输出:
15 0
14 1
13 2
12 3
11 4
10 5
9 6
8 7
7 8
IndexError: list assignment index out of range`
到目前为止,我想到的唯一解决方案是为列表中的每个项目创建一个变量,然后以这种方式删除它们,但我听说通常不需要这样做。有什么办法可以做到这一点吗
Tags:
一种方法是向后逐步浏览列表
相关问题 更多 >
编程相关推荐