(Python) 列表索引超出范围 - 迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20819504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(Python) List index out of range - iteration
提问by user3142764
for i in range(len(lst)):
if lst[i][0]==1 or lst[i][1]==1:
lst.remove(lst[i])
return lst
This gives "IndexError: list index out of range" Why is this happening?
这给出了“IndexError:列表索引超出范围”为什么会发生这种情况?
采纳答案by Tim Pietzcker
You're modifying the list you're iterating over. If you do that, the size of the list shrinks, so eventually lst[i]will point beyond the list's boundaries.
您正在修改正在迭代的列表。如果这样做,列表的大小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
It's safer to construct a new list:
构造一个新列表更安全:
return [item for item in lst if item[0]!=1 and item[1]!=1]
回答by jonrsharpe
You shouldn't removeitems from the listas you iterate over it; that changes the indices of all subsequent items, hence the IndexError. You could try a simple list comprehension:
remove在list迭代它时,您不应该从 中取出项目;这会更改所有后续项目的索引,因此IndexError. 你可以尝试一个简单的列表理解:
lst = [item for item in lst if (item[0] != 1 and item[1] != 1)]
回答by user2537827
The problem is that you remove items in the list which reduces its size. What you have to do is make an array with the indexes you want to remove and remove them backwards.
问题是您删除了列表中的项目,从而减小了其大小。您需要做的是使用要删除的索引创建一个数组,然后向后删除它们。
Another way would be to create a temporary list that you would add the elements you don't want to delete and then overwrite your initial list with the list containing all the elements you want to keep.
另一种方法是创建一个临时列表,您可以添加不想删除的元素,然后用包含您要保留的所有元素的列表覆盖初始列表。
回答by CodeHard_or_HardCode
By process of deduction I have concluded that your lst looks something like this:
通过演绎过程,我得出结论,你的 lst 看起来像这样:
lst = [ ...[val1, val2], [val1, val2], [val1, val2]... ]
lst = [ ...[val1, val2], [val1, val2], [val1, val2]... ]
I think what happened here is you confused your 'for i in range', with a 'for i in' (I have done this many times also.)
我认为这里发生的事情是您将“for i in range”与“for i in”混淆了(我也做过很多次)。
The line where your error is occurring is:
lst.remove(lst[i])
发生错误的行是:
lst.remove(lst[i])
You can correct this by simply changing your code like so:
您可以通过简单地更改代码来纠正此问题:
for i in lst:
if i[0] ==1 or i[1] ==1:
lst.remove(lst[i])
return lst
The way your code was structured before list[i] didn't make any sense, your i was a number greater than the number of two-value-items in lst.
你的代码在 list[i] 之前的结构方式没有任何意义,你的 i 是一个大于 lst 中二值项目数的数字。
=D
=D
回答by jp rathore
Generally it means that you are providing an index for which a list element does not exist.
通常,这意味着您提供的索引不存在列表元素。
E.g, if your list was [12, 32, 50, 71], and you asked for the element at index 10, you would be well out of bounds and receive an error, as only elements 0 through 3 exist.
例如,如果您的列表是 [12, 32, 50, 71],并且您要求索引 10 处的元素,那么您将完全超出范围并收到错误消息,因为只有元素 0 到 3 存在。

