简体   繁体   English

为什么在此代码中出现IndexError:list index超出范围?

[英]Why am I getting IndexError: list index out of range in this code?

Im trying to make a two way bubble sort. 我正在尝试进行双向气泡排序。 on odd iterations, it bubbles and sorts to the right, on even iterations, it bubbles and sorts to the left. 在奇数次迭代中,它冒泡并在右侧排序,在奇数次迭代中,它冒泡并在左侧排序。

def main():

    myList = [10,9,8,7,6,5,4,3,2,1]
    lengthOfList = len(myList)
    firstThresh = 0
    lastThresh = lengthOfList
    oddPass = True
    while firstThresh <= lastThresh:
        if oddPass == True:
            for index in myList[firstThresh:lastThresh]:                
                if myList[index] > myList[index+1]:      <==================
                    temp = myList[index]
                    myList[index] = myList[index+1]
                    myList[index+1] = temp
                    print(myList)
                    oddPass = False
            lastThresh -= 1
        else:
            for index in reversed(myList[firstThresh:lastThresh]):
                if myList[index] < myList[index-1]:
                    temp = myList[index]
                    myList[index] = myList[index-1]
                    myList[index+1] = temp
                    print(myList)
                   oddPass = False
            firstThresh += 1
main()

The error: line 22, in bubbleSort2Way if myList[index] > myList[index+1]: IndexError: list index out of range 错误:如果myList [index]> myList [index + 1],则在bubbleSort2Way中的第22行:IndexError:列表索引超出范围

I put the arrow where the problem is. 我把箭头放在问题所在的位置。 I am a beginner at programming so if its obvious im sorry! 我是编程的初学者,所以很抱歉! Any help would be great! 任何帮助将是巨大的!

The line 线

for index in myList[firstThresh:lastThresh]:

is using the values from myList , not the index. 正在使用myList的值,而不是索引的值。 So the code is trying to access the 11th element (index "10") on a list with only 10 elements and getting an out of range error. 因此,代码尝试访问仅包含10个元素的列表中的第11个元素(索引“ 10”),并出现超出范围的错误。

To use the index in your loop, change this line to 要在循环中使用索引,请将此行更改为

for index, value in enumerate(myList[firstThresh:lastThresh]):

暂无
暂无

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

相关问题 为什么我收到 IndexError: list index out of range 这个函数? - Why am I getting IndexError: list index out of range for this function? 为什么我收到 IndexError: list index out of range - Why am I getting IndexError: list index out of range 当我的代码似乎在范围内时,为什么会出现 IndexError: list index out of range? - Why am I getting an IndexError: list index out of range when my code appears to be within the range? 我收到IndexError:列表索引超出范围 - I am getting IndexError: list index out of range 为什么收到此错误“ IndexError:字符串索引超出范围” - Why am I getting this Error “IndexError: string index out of range” 为什么会出现 IndexError:字符串索引超出范围? - Why am I getting an IndexError: string index out of range? 为什么我收到此错误? twitter_list_dict.append(line [0])IndexError:列表索引超出范围 - why am I getting this error? twitter_list_dict.append(line[0]) IndexError: list index out of range 为什么我会收到此错误? IndexError:列表索引超出范围 - Why am I receiving this error? IndexError: list index out of range 为什么在云上训练时会收到“IndexError: list index out of range”? - Why am I getting "IndexError: list index out of range" when training on the cloud? 为什么我在 Python3 中收到此错误:IndexError:列表索引超出范围 - Why am I getting this error in Python3: IndexError: list index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM