Python 索引器错误:使用while循环列出超出范围的索引
我一直在尝试修复此问题,但每次都会出现相同的错误消息:Python 索引器错误:使用while循环列出超出范围的索引,python,Python,我一直在尝试修复此问题,但每次都会出现相同的错误消息: 如果我没听错的话,你是在给一个数字列表排序。有一个内置的Python方法可以实现这一点,它经过了优化,可以使速度更快并使用最少的内存,它被称为sorted 不要运行订单号列表,请尝试: print(sorted(number_list)) 如果这是一种家庭作业,并且不允许您使用内置的排序,出于教育目的,我建议您使用一种称为冒泡排序的排序算法,这里有更多信息-。更新您的代码以包含两条打印语句,表明j增加到超过列表长度。然后是“while n
如果我没听错的话,你是在给一个数字列表排序。有一个内置的Python方法可以实现这一点,它经过了优化,可以使速度更快并使用最少的内存,它被称为
sorted
不要运行订单号列表
,请尝试:
print(sorted(number_list))
如果这是一种家庭作业,并且不允许您使用内置的
排序
,出于教育目的,我建议您使用一种称为冒泡排序的排序算法,这里有更多信息-。更新您的代码以包含两条打印语句,表明j增加到超过列表长度。然后是“while number_列表[i]
需要更改为:
number_list.append(int(item))
因为您无法将字符串与“that not the key”进行比较,我的意思是。正如我所指出的,错误在下一行。您不使用
排序(数字列表)有什么原因吗
?我想这是OP的家庭作业,他们不能使用内置程序,比如排序的
。我知道内置程序的存在,但是我从Python开始,我更愿意一步一步地开发这个程序。我想把一个数字列表从小到大排序。我手动介绍了list,然后我用order_number_list函数对其求值。因此,首先我进行while循环,以便将每个索引与最后一个索引进行比较。然后,当一个索引被识别为小索引时,我使用del语句将其从原始列表中删除,并使用append将其存储在新列表中。@GonzaloMolina这不是一个非常有效的排序方法如果你想知道Python是如何被使用的,那么你就可以知道它是如何被使用的。是的!我知道for循环比while循环好,但是明智的是,我正在测试它有多棘手。谢谢你们,真的…我一点也不明白你们的意思,但是,如果你如果有一个包含5个数字的列表,则列表[5]存在。由于我使用del修改旧列表,新列表将使用其新长度进行计算。python标签从0开始。列表中的第一项python调用0。在上面的示例中,如果在j=0时打印(数字\u列表[j]),它将打印“1”“我知道列表标签是怎么工作的,但你这样肯定对我什么也没说。但明智的,谢谢你。终于!谢谢你,我会检查新的线路并与我的线路进行比较。我无法理解的是:如果我用:int(input())引入了列表输入,为什么我要使用:number_list.append(int(item))?@GonzaloMolina您只对列表长度的输入使用int(input()),而不是对列表中的每个项目使用int(input())。回顾一下问题中的原始代码,int()在输入中的唯一用途是列表的长度,而不是列表中的每一项。非常感谢您,很抱歉让您这么容易产生疑问。只有几点值得提醒的疑问:为什么我们要在-->current_flow=[]中放入“数字”?我不理解las if和else语句。@GonzaloMolina写得很糟糕,它应该是空白的,正如你所建议的,下面的行current_low[0]=number_list[0]应该更改为current_low。append(number_list[0])我这样做是为了分配它,而不是追加它。再次感谢!应该有更多像你这样的人,对那些刚开始编程的人有耐心
number_list=[1,2,3,4,5]
list_lenght=5
#=int(input("List lenght: "))
#while len(number_list)<list_lenght:
# item=input("Enter new item to the list:")
# number_list.append(item)
# print(number_list)
print("That's your number list: ",number_list)
number_list_final=[]
def order_number_list(number_list):
i=0
j=1
while (j)<=(len(number_list)):
while number_list[i]<=number_list[j]:
print (j)
print (i)
j=j+1
i=j
j=i+1
final_item=number_list[i]
number_list_final.append(final_item)
del number_list[i]
order_number_list(number_list)
order_number_list(number_list)
print(number_list_final)
That's your number list: [1, 2, 3, 4, 5]
1
0
2
0
3
0
4
0
Traceback (most recent call last):
File "C:/Python3/numberlist.py", line 30, in <module>
order_number_list(number_list)
File "C:/Python3/numberlist.py", line 19, in order_number_list
while number_list[i]<=number_list[j]:
IndexError: list index out of range
number_list=[]
list_lenght=int(input("List length: "))
while len(number_list)<list_lenght:
item=input("Enter new item to the list:")
number_list.append(int(item))
print(number_list)
print("That's your number list: ",number_list)
number_list_final=[]
def order_number_list(number_list):
current_low = ["number"]
current_low[0] = number_list[0]
x = 1
current_low_pos = 0
while x < len(number_list):
if current_low[0] > number_list[x]:
current_low[0] = number_list[x]
current_low_pos = x
x = x + 1
del number_list[current_low_pos]
if number_list == []:
remaining_list = []
else:
remaining_list = order_number_list(number_list)
return (current_low + remaining_list)
number_list_final = order_number_list(number_list)
print(number_list_final)
number_list.append(item)
number_list.append(int(item))
number_list_final = order_number_list(number_list)
order_number_list(number_list)