您正在修改要遍历的列表。如果这样做,列表的大小将缩小,因此最终lst[i]
将指向列表的边界之外。
>>> lst = [1,2,3]
>>> lst[2]
3
>>> lst.remove(1)
>>> lst[1]
3
>>> lst[2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
构造新列表更安全:
return [item for item in lst if item[0]!=1 and item[1]!=1]