[英]List index out of range using While loop
This is my first time posting and I am a bit of newbie so please excuse any errors in my question. 这是我的第一次发布,我是新手,所以请原谅我的问题中的任何错误。 I have to create a function that uses the While loop and takes in a list of numbers and adds each number to a new list until the a specific number is reached.
我必须创建一个使用While循环并接收数字列表并将每个数字添加到新列表中的函数,直到达到特定数字为止。 For example - I want each number in a list to be added until the number 5 is reached in the list.
例如-我希望添加列表中的每个数字,直到列表中的数字达到5。 If the number 5 is not in the list, then I would get the entire list as is.
如果数字5不在列表中,那么我将按原样获得整个列表。 The issue I am having is with this last part.
我遇到的问题是最后一部分。 My current code, posted below, can give me a new list of numbers that stops at the number 5 but I get the "List index out of range" error when the number 5 is not included in the list.
我下面发布的当前代码可以给我一个新的数字列表,该列表以数字5停止,但是当数字5不包括在列表中时,出现“列表索引超出范围”错误。 I am sure its something small that I am not factoring in. Any help or guidance on what I am doing wrong would be greatly appreciated.
我确信这是我不应该考虑的小问题。对于我在做错事的任何帮助或指导,将不胜感激。
def sublist(x):
i = 0
num_list = []
while x[i] != 5:
num_list.append(x[i])
i = i + 1
return num_list
print(sublist([1, 2, 5, 7])) #works fine
print(sublist([1, 2, 7, 9])) #list index out of range error
As others have pointed out, it is best to use a for
loop when you already know the number loops you need (eg: len(x)
in your case). 正如其他人指出的那样,最好在您已经知道所需的数字循环(例如您的情况下为
len(x)
时使用for
循环。
If you still really want to use a while
loop instead, you will need to check every loop to see if you have checked every item in the old list, and exit the loop if you do. 如果您仍然真的想使用
while
循环,则需要检查每个循环以查看是否检查了旧列表中的每个项目,如果已经检查则退出循环。 Here's what your code could look like if you're using a while
loop: 如果使用
while
循环,则代码如下所示:
def sublist(x):
i = 0
num_list = []
original_length = len(x)
while i < original_length and x[i] != 5:
num_list.append(x[i])
i = i + 1
return num_list
print(sublist([1, 2, 5, 7])) #works fine
print(sublist([1, 2, 7, 9])) #now also works fine
EDIT: I originally had the check for i < original_length
inside the loop, I've changed it to be inside the while
condition. 编辑:我最初在循环内检查了
i < original_length
,我将其更改为while
条件。 BUT BE CAREFUL because the check MUST come before x[i] != 5
. 但是要小心,因为支票必须在
x[i] != 5
。 What I mean is that using this will work: 我的意思是,使用它会起作用:
while i < original_length and x[i] != 5:
But this will not: 但这不会:
while x[i] != 5 and i < original_length:
This should do the trick. 这应该可以解决问题。 If
n
(5 in your example) is in the list x
it will take the list until that point. 如果
n
(在您的示例中为5)在列表x
则它将一直使用列表直到该点。 Else, it will take the whole list. 否则,它将占据整个列表。 This isn't the most pythonic option, though, probably someone knows a better way.
但是,这不是最pythonic的选项,可能有人知道更好的方法。
def sublist(x, n):
num_list=[]
if n in x:
for i in x:
while i!=n:
num_list.append(i)
else:
num_list=x
return num_list
Your loop will not stop if there is no 5
in the list, so the index i
will be equal to the length of x
(length of x
is out of range) in the iteration when the error happens. 如果列表中没有
5
,循环将不会停止,因此发生错误时,索引i
将等于迭代中x
的长度( x
长度超出范围)。
When looping with python, it's better to use for in
loops: 使用python循环时,最好
for in
循环中使用:
def sublist(x):
num_list = []
for n in x:
if n != 5:
num_list.append(n)
else:
break
return num_list
print(sublist([1, 2, 5, 7])) # [1, 2]
print(sublist([1, 2, 7, 9])) # [1, 2, 7, 9]
Try this 尝试这个
def sublist(x):
x.sort()
limit_num = 5
if limit_num not in x:return x
return x[:x.index(limit_num)]
print(sublist([1, 2, 5, 7]))
print(sublist([1, 2, 7, 9]))
Result:[1, 2]
[1, 2, 7, 9]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.