IndexError: list index out of range in python for strings

▼魔方 西西 提交于 2019-12-10 23:59:19

问题


I wanted to remove the word "hello" from this array, but I get the "index out of bounds" error. I checked the range of len(token); it was (0,5).

Here is the code:

token=['hi','hello','how','are','you']

stop='hello'

for i in range(len(token)):
    if(token[i]==stop):
        del(token[i])

回答1:


You're getting an index out of bounds exception because you are deleting an item from an array you're iterating over.

After you delete that item, len(token) is 4, but your for loop is iterating 5 times (5 being returned from the initial len(token)).

There are two ways to solve this. The better way would be to simply call

token.remove(stop)

This way won't require iterating over the list, and will automatically remove the first item in the array with the value of stop.

From the documentation:

list.remove(x): Remove the first item from the list whose value is x. It is an error if there is no such item.

Given this information, you may want to check if the list contains the target element first to avoid throwing a ValueError:

if stop in token:
    token.remove(stop)

If the element can exist multiple times in the list, utilizing a while loop will remove all instances of it:

while stop in token:
    token.remove(stop)

If you need to iterate over the array for some reason, the other way would be to add a break after del(token[i]), like this:

for i in range(len(token)):
    if(token[i]==stop):
        del(token[i])
        break



回答2:


It's not recommended to delete a list element when iterating over this list. I'm not sure what you intend but you could create a new list without the stop

token=['hi','hello','how','are','you']
stop='hello'

new_tokens = []
for i in range(len(token)):
    if(token[i]!=stop):
        new_tokens.append(token[i])

or create a list with everything until stop is reached:

token=['hi','hello','how','are','you']
stop='hello'

new_tokens = []
for i in range(len(token)):
    if(token[i]!=stop):
        new_tokens.append(token[i])
    else:
        break

But never delete elements from a list you are iterating over because then the length of the list is modified but the range is not.




回答3:


The reason you are getting this error is two fold:

  • you are using the anti pattern in Python of range(len(sequence)). You should use for index, value in enumerate(sequence)
  • You are mutating a sequence as you iterate across it.

The call to range(len(...)) is only evaluated once. So when you star it evaluates to 5. Once you remove your stop word the list no longer has 5 elements so token[4] results in an IndexError




回答4:


Once you delete an item, there are no longer as many items in the list as there were originally, so i will get too big. Also, if you delete the item at index i, then the element that used to be at i+1 will now be at index i, but your code won't check it, since it goes ahead and increments i.




回答5:


Use break statement after deleting because you are modifying the same list in which you are iterating.

for i in range(len(token)):
  if(token[i]==stop):
    del(token[i])
    break 



回答6:


I don't disagree with any of the other answers, but the most Pythonic solution is to get rid of the loop entirely and replace it with one line:

token.remove(stop)

That will remove the first occurrence of 'hello' from the list.



来源:https://stackoverflow.com/questions/35517360/indexerror-list-index-out-of-range-in-python-for-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!