(most recent call last):
File "", line 2, in
if x[i] == 1:
IndexError: list...index out of range
>>> x
[2, 2, 1, 1]
不但没有解决问题,反而引发了一个异常,下标越界。...== 1:
IndexError: list index out of range
好像真的是这个问题,为了更好地理解这个问题,看下面的代码
>>> x = [(0,1),(1,1),(2,1),(3,1...[1] == 1:
IndexError: list index out of range
>>> x
[(1, 1), (3, 1), (5, 1)]
好了,问题的原因已经确定了,那么正确的代码该怎么写呢...既然从列表中间位置删除元素会导致后面的元素索引发生改变,那么就从后往前删除好了。