简体   繁体   English

列表索引超出范围:python

[英]list index out of range: python

I have a list which has these elements(list consists str(elements)): 我有一个包含这些元素的列表(列表由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. 如果元素是-xi想要保留它,那么我取最后2个元素,如果间隙为1,则删除最后一个元素之前的元素。 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. 最后2个元素不包含char“-”,但似乎包含。 Furthermore after the 8 loop script crashes: 此外,在8个循环脚本崩溃之后:

 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. 如果从包含十个项目的l1开始,则k将从0变为9,但是如果从l1删除一个项目,则l1[9]不再存在。

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? 我也认为if "-" in (l1[-k] or l1[-k-1]): "-"正在测试"-"是否在l1[-k]l1[-k-1]的逻辑或中,我认为这不是您的意图?

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. 是是IndexError:列表索引超出范围。 Because you are trying to access l1[-10] at line 5, which does not exist in l1 after 8 iterations. 因为您尝试在第5行访问l1 [-10],所以在8次迭代后l1中不存在。

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. 我刚刚打印了清单l1。 After 8 iterations,int(l1[-k-2] is not accessible. This is the reason why u are getting Index error 经过8次迭代后,int(l1 [-k-2]无法访问。这就是u出现索引错误的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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