简体   繁体   中英

list index out of range: python

I have a list which has these elements(list consists str(elements)):

['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']

and I want to process it to have an output like this

['-0', '1#', '15#']

If the element is -xi want to leave it there so I take the last 2 elements and if the gap is 1 then remove the element before the last element. Here is the code:

            for k in range(len(l1)):
                if "-" in (l1[-k] or l1[-k-1]):
                    print("debuggggg")
                    pass
                elif(int(l1[-k]) - int(l1[-k-1])== 1 and int(l1[-k]) - int(l1[-k-2])== 2) :
                    a= l1[-2]
                    print(a)
                    l1.remove(a)
                    #print("debug 2")
                elif(int(l1[-k]) - int(l1[-k-1])== 1):
                    a= l1[-2]
                    l1.remove(a)
                    l1[-2] = l1[-2] +"#"
                    l1[-1] = l1[-1] +"#"
                    print("3")
                #elif(type(l1[-2]) is str):
                    #pass

Problem is here :

 debuggggg
    14
    13
    12
    11
    10
    9
    8

The last 2 elements do not include char "-" but it seems that they do. Furthermore after the 8 loop script crashes:

 elif(int(l1[-k]) - int(l1[-k-1])== 1 and int(l1[-k]) - int(l1[-k-2])== 2) :
IndexError: list index out of range

but it is not out the range of the list. What is the problem?

You're removing items from the list. If you start with l1 containing ten items then k is going to go from 0 to 9, but if you remove an item from l1 then l1[9] no longer exists.

Also I think if "-" in (l1[-k] or l1[-k-1]): is testing whether "-" is in the logical OR of l1[-k] and l1[-k-1] , which I don't think is what you intended?

You should rather have a source list that you iterate over and a results list that you insert elements into.

source_list = ['-1', '1', '2', '3']
results_list = []

for item in source_list:
    # Do some tests, and put items into the results list if you want them.
    # I think this is what you want, although I couldn't understand from your question.
    if '-' in item:
        results_list.append(item)
    else:
        results_list.append(item + '#')

If there is some other requirement then let me know, and I will try to adapt my example.

Your second question

but it is not out the range of the list. What is the problem?

It is just out of range error.

l1[-len(l1)-1]

raises this error. You are doing this in your code.

for k in range(len(l1)):
    ...
    elif(int(l1[-k]) - int(l1[-k-1])== 1 and int(l1[-k]) - int(l1[-k-2])== 2) :#<--here(l1[-k-2])

yes It is IndexError: list index out of range. Because you are trying to access l1[-10] at line 5, which does not exist in l1 after 8 iterations.

See the following debug info:

>>> l1 = ['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
>>> for k in range(len(l1)):
...     if "-" in (l1[-k] or l1[-k-1]):
...         print("debuggggg")
...         pass
...     elif(int(l1[-k]) - int(l1[-k-1])== 1 and int(l1[-k]) - int(l1[-k-2])== 2) :
...         a= l1[-2]
...         print(a)
...         l1.remove(a)
...         print l1
...         #print("debug 2")
...     elif(int(l1[-k]) - int(l1[-k-1])== 1):
...         a= l1[-2]
...         l1.remove(a)
...         l1[-2] = l1[-2] +"#"
...         l1[-1] = l1[-1] +"#"
...         print("3")
...     #elif(type(l1[-2]) is str):
...         #pass
... 
debuggggg
14
['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '15']
13
['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '15']
12
['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '15']
11
['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '15']
10
['-0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '15']
9
['-0', '1', '2', '3', '4', '5', '6', '7', '8', '15']
8
['-0', '1', '2', '3', '4', '5', '6', '7', '15']
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
IndexError: list index out of range

I have just printed list l1. After 8 iterations,int(l1[-k-2] is not accessible. This is the reason why u are getting Index error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM