使用while循环迭代一个列表,并在达到给定值时停止。

huangapple go评论72阅读模式
英文:

Using a While Loop to Iterate a List and Stop at a given value

问题

Sure, here's the translated code part:

def stop_at_four():
    list_ = [3, 6, 4, 1, 3]
    accum_lst = []
    accum_var = 0
    
    while list_[accum_var] != 4:
        accum_lst.append(list_[accum_var])
        accum_var += 1
    return accum_lst

If you have any further questions or need assistance with code improvement, feel free to ask.

英文:

Question

Write a function called stop_at_four that iterates through a list of numbers. Using a while loop, append each number to a new list until the number 4 appears. The function should return the new list.

My answer

def stop_at_four():  
    list_=[3,6,4,1,3]
    accum_lst=[]
    accum_var=0
    
    while list_[accum_var] !=4:
        accum_lst.append(list_[accum_var])
    accum_var+=1
    return

P.S. any criticism on how to improve my code efficiency would be greatly appreciated.

答案1

得分: 3

你应该将列表作为参数传递并返回新的列表。此外,accum_var的增加必须在while循环内部,并且你应该检查是否已经到达列表的末尾,以防止列表索引超出范围:

def stop_at_four(my_list):
    accum_lst = []
    accum_var = 0

    while (accum_var < len(my_list)) and (my_list[accum_var] != 4):
        accum_lst.append(my_list[accum_var])
        accum_var += 1
    return accum_lst

print(stop_at_four([3, 6, 4, 1, 3]))
英文:

You should give the list as a parameter and return the new list. Also the increment of accum_var have to be inside the while loop and you should check whether if you reached the end of the list to prevent a list index out of range:

def stop_at_four(my_list):
    accum_lst=[]
    accum_var=0

    while (accum_var &lt; len(my_list)) and (my_list[accum_var] != 4):
        accum_lst.append(my_list[accum_var])
        accum_var+=1
    return accum_lst

print(stop_at_four([3,6,4,1,3]))

答案2

得分: 1

这段代码会在所有可能的条件下运行。

def stop_at_four(lst):
    n = 0
    new_lst = []
    if len(lst) == 0:
        return new_lst
    else:
        while lst[n] != 4 and n < len(lst):
            new_lst.append(lst[n])
            n += 1
        return new_lst

lst = [3, 5, 7, 9.5, 7, 1, 4]
print(stop_at_four(lst))
英文:

Well... this code will run in all possible conditions`

def stop_at_four(lst):
    n = 0
    new_lst = []
    if len(lst) == 0:
        return new_lst
    else:
        while lst[n] != 4 and n &lt; len(lst):
            new_lst.append(lst[n])
            n += 1
        return new_lst
lst = [3, 5, 7, 9.5, 7, 1, 4]
print(stop_at_four(lst))`

.

答案3

得分: 0

根据Arunesh Singh提到的,正确的代码块应该是

def stop_at_four():
    list_ = [3, 6, 4, 1, 3]
    accum_lst = []
    accum_var = 0

    while list_[accum_var] != 4:
        accum_lst.append(list_[accum_var])
        accum_var += 1
    return accum_lst

递增应该是while循环的一部分。

英文:

As mentioned by Arunesh Singh, the correct code block should be

def stop_at_four():  
    list_=[3,6,4,1,3]
    accum_lst=[]
    accum_var=0

    while list_[accum_var] !=4:
        accum_lst.append(list_[accum_var])
        accum_var+=1
    return accum_lst

Incrementation should be part of while loop.

答案4

得分: 0

它对我有效!!! 只是试一试...

def stop_at_four():
    list_=[3,6,4,1,3]
    accum_lst=[]
    accum_var=0

    while list_[accum_var] != 4 :
        accum_lst.append(list_[accum_var])
        accum_var += 1
    return accum_lst

print(stop_at_four())
英文:

It works for me!!! Just give a try...

def stop_at_four():
    list_=[3,6,4,1,3]
    accum_lst=[]
    accum_var=0

    while list_[accum_var] != 4 :
        accum_lst.append(list_[accum_var])
        accum_var += 1
    return accum_lst

print(stop_at_four())

答案5

得分: 0

这个函数更有效!

def stop_at_four(lst):
    n = 0
    new_lst = []
    while lst[n] != 4:
        new_lst.append(lst[n])
        n += 1
    return new_lst

lst = [1,2,4,6,12]
print(stop_at_four(lst))
英文:

This works better!

def stop_at_four(lst):
    n = 0
    new_lst = []
    while lst[n] != 4:
        new_lst.append(lst[n])
        n += 1
    return new_lst
lst = [1,2,4,6,12]
print(stop_at_four(lst))

答案6

得分: 0

def stop_at_four(lis):
new_list = []
start = 0
while start < len(lis) and lis[start] != 4:
new_list.append(lis[start])
start += 1
return new_list

list1 = [1, 6, 2, 3, 9]
print(stop_at_four(list1))

英文:
def stop_at_four(lis):
    new_list = []
    start = 0
    while start &lt; len(lis) and lis[start] != 4:
        new_list.append(lis[start])
        start += 1
    return new_list    
    
list1 = [1, 6, 2, 3, 9]     
print(stop_at_four(list1))

答案7

得分: 0

以下是翻译好的内容:

以另一种方式处理了这个问题,但不确定是否有使用 in 运算符的限制:

def stop_at_four(lst):
    if 4 not in lst:
        return lst
    idx = 0
    accum_list = []
    while lst[idx] != 4:
        accum_list.append(lst[idx])
        idx += 1
    return accum_list
英文:

Approached this another way but not sure if there is a restriction on using the in operator:

def stop_at_four(lst):
    if 4 not in lst:
        return lst
    idx = 0
    accum_list = []
    while lst[idx] != 4:
        accum_list.append(lst[idx])
        idx += 1
    return accum_list

答案8

得分: 0

Here's the translated code:

尝试使用这个代替

def stop_at_four(lst):
    sublist = []
    y = (num for num in lst)
    num = next(y, 4)
    while num != 4:
        sublist.append(num)
        num = next(y, 4)
    return sublist
英文:

Try this instead:

def stop_at_four(lst):
    sublist = []
    y = (num for num in lst)  
    num = next(y, 4)  
    while num != 4:
        sublist.append(num)
        num = next(y,4)  
return sublist

答案9

得分: 0

这也有效:

def stop_at_four(lst):
    i = 0 
    new_lst = []
    while i < len(lst):
        if lst[i] != 4 :
            new_lst.append(lst[i])
        else :
            break
        i = i + 1
    return new_lst
英文:

This works well also :

def stop_at_four(lst):
i = 0 
new_lst = []
while i &lt; len(lst):
    if lst[i] != 4 :
        new_lst.append(lst[i])
    else :
        break
    i = i + 1
return new_lst

huangapple
  • 本文由 发表于 2020年1月6日 16:13:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定