Python, Lists error: IndexError: list index out of range
Python, Lists error: IndexError: list index out of range
我一直在开发一个程序,该程序接受一个字符串并将其按字母顺序排序(冒泡排序)。我编写了一段简单的代码来开始我的工作,但是在 运行 之后不久我在 shell 中遇到了一个问题。
我的代码:
aList = ["b", "a", "d", "c"]
compOne = 0
compTwo = 1
sorting = True
while sorting == True:
print (aList)
sortingList = []
sortingList.insert(compOne, aList[compOne])
sortingList.insert(compTwo, aList[compTwo])
aList[compOne] = sorted(sortingList)[compOne]
aList[compTwo] = sorted(sortingList)[compTwo]
print (aList)
print("__________________________")
compOne = compOne + 1
compTwo = compTwo + 1
我的想法是它会一直通过列表交换项目直到它按字母顺序排列(我还没有关闭 while 循环,当我通过这个问题时我会继续这样做)
我想要的输出:
['a', 'b', 'd', 'c']
__________________________
['a', 'b', 'd', 'c']
__________________________
['a', 'b', 'c', 'd']
__________________________
ETC(Will repeat because i haven't closed the while loop)
我得到的错误输出:
['a', 'b', 'd', 'c']
__________________________
Traceback (most recent call last):
File "C:/Users/MyName/Desktop/Python Programs/Projects/Bubble Sort/Test File 4.py", line 10, in <module>
aList[compTwo] = sorted(sortingList)[compTwo]
IndexError: list index out of range
正如您所知道的,它来到了要比较的第二组项目并遇到了这个错误。由于某些原因
aList[compOne] = sorted(sortingList)[compOne]
很好,但 aList[compTwo] = sorted(sortingList)[compTwo]
不行(建议它们是相似的代码)
我已经调查了这个问题 1 个半小时,但我还没有找到解决方案,如果你能告诉我问题并深入解释为什么会这样,我将不胜感激。(我不只想要答案,但我想要解释我做错了什么)与此同时,我正在等待答案和解释,我会自己调查问题。谢谢。 (写于python 3.6.1)
CompOne
从 0
开始
CompTwo
从 1
开始
你 add 1
一直到这两个 - 直到 CompOne
寻址字符串的最后一个字符(这很好)并且 CompTwo
尝试访问字符 after 字符串的最后一个字符 -> Indexerror
.
我不确定我是否正确理解了你试图做的事情,最让我困惑的是:
- 你在每个 while-loop-round 中将你的
sortingList
重新实例化为一个空列表?
- 在每个 while 循环中,您将 char
n
和 'n+1' 放入数组的某些索引处
- 您的 sortingList 已排序两次 (sorted(.....)),这会创建两次新列表,这是非常不必要的 - 您可以简单地对其排序一次并将其存储在您访问的临时变量中。你为什么在这里使用
.insert
和索引,由于 sortedList=[]
之前的几行 ,列表在你放入其中的两个字符旁边是空的
您需要注意输入数组的长度,CompTwo
不能超过。
我一直在开发一个程序,该程序接受一个字符串并将其按字母顺序排序(冒泡排序)。我编写了一段简单的代码来开始我的工作,但是在 运行 之后不久我在 shell 中遇到了一个问题。 我的代码:
aList = ["b", "a", "d", "c"]
compOne = 0
compTwo = 1
sorting = True
while sorting == True:
print (aList)
sortingList = []
sortingList.insert(compOne, aList[compOne])
sortingList.insert(compTwo, aList[compTwo])
aList[compOne] = sorted(sortingList)[compOne]
aList[compTwo] = sorted(sortingList)[compTwo]
print (aList)
print("__________________________")
compOne = compOne + 1
compTwo = compTwo + 1
我的想法是它会一直通过列表交换项目直到它按字母顺序排列(我还没有关闭 while 循环,当我通过这个问题时我会继续这样做) 我想要的输出:
['a', 'b', 'd', 'c']
__________________________
['a', 'b', 'd', 'c']
__________________________
['a', 'b', 'c', 'd']
__________________________
ETC(Will repeat because i haven't closed the while loop)
我得到的错误输出:
['a', 'b', 'd', 'c']
__________________________
Traceback (most recent call last):
File "C:/Users/MyName/Desktop/Python Programs/Projects/Bubble Sort/Test File 4.py", line 10, in <module>
aList[compTwo] = sorted(sortingList)[compTwo]
IndexError: list index out of range
正如您所知道的,它来到了要比较的第二组项目并遇到了这个错误。由于某些原因
aList[compOne] = sorted(sortingList)[compOne]
很好,但 aList[compTwo] = sorted(sortingList)[compTwo]
不行(建议它们是相似的代码)
我已经调查了这个问题 1 个半小时,但我还没有找到解决方案,如果你能告诉我问题并深入解释为什么会这样,我将不胜感激。(我不只想要答案,但我想要解释我做错了什么)与此同时,我正在等待答案和解释,我会自己调查问题。谢谢。 (写于python 3.6.1)
CompOne
从 0
CompTwo
从 1
你 add 1
一直到这两个 - 直到 CompOne
寻址字符串的最后一个字符(这很好)并且 CompTwo
尝试访问字符 after 字符串的最后一个字符 -> Indexerror
.
我不确定我是否正确理解了你试图做的事情,最让我困惑的是:
- 你在每个 while-loop-round 中将你的
sortingList
重新实例化为一个空列表? - 在每个 while 循环中,您将 char
n
和 'n+1' 放入数组的某些索引处 - 您的 sortingList 已排序两次 (sorted(.....)),这会创建两次新列表,这是非常不必要的 - 您可以简单地对其排序一次并将其存储在您访问的临时变量中。你为什么在这里使用
.insert
和索引,由于sortedList=[]
之前的几行 ,列表在你放入其中的两个字符旁边是空的
您需要注意输入数组的长度,CompTwo
不能超过。