【问题标题】:Using a While Loop to Iterate a List and Stop at a given value使用 While 循环迭代列表并在给定值处停止
【发布时间】:2020-04-23 19:05:17
【问题描述】:

问题

编写一个名为 stop_at_four 的函数,它遍历一个数字列表。使用 while 循环,将每个数字附加到新列表中,直到数字 4 出现。该函数应返回新列表。

我的回答

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_var += 1 必须在 while 内。

标签: python list while-loop


【解决方案1】:

您应该将列表作为参数并返回新列表。此外 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]))

【讨论】:

  • 显然你不能使用&amp;你必须使用and
  • and 是逻辑运算符
【解决方案2】:

嗯...此代码将在所有可能的条件下运行`

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))`

.

【讨论】:

    【解决方案3】:

    正如 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循环的一部分。

    【讨论】:

    • 你什么都不返回
    • 是的,打错了。
    【解决方案4】:

    对我有用!!!试试看吧……

    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】:

      这样效果更好!

      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】:
      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))
      

      【讨论】:

        【解决方案7】:

        以另一种方式接近,但不确定使用 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
        

        【讨论】:

          猜你喜欢
          • 2015-10-20
          • 1970-01-01
          • 2021-10-31
          • 1970-01-01
          • 2022-10-06
          • 1970-01-01
          • 1970-01-01
          • 2017-09-22
          • 1970-01-01
          相关资源
          最近更新 更多