【问题标题】:Python: How to For Loop a list and append to new listPython:如何循环列表并附加到新列表
【发布时间】:2019-04-13 05:58:02
【问题描述】:

练习我的蟒蛇。

任务: 遍历列表 A 并创建一个新列表,其中仅包含 0-5 之间的列表 A 中的项目。

我做错了什么

a = [100, 1, 10, 2, 3, 5, 8, 13, 21, 34, 55, 98]


def new_list(x):

    for item in range(len(x)):
        new = []

        if x[item] < 5 and x[item] > 0:
            (new.append(item))
            return new


print(new_list(a))

我刚刚收到[1] 作为答案。

【问题讨论】:

  • new = [] 移动到你的 for 循环之前

标签: python list for-loop append


【解决方案1】:

只是一个建议!

  1. 空列表在For循环内,意味着每次迭代都会创建一个新的空列表

  2. 'return' 也在 for 循环内,它不太理想,您希望在循环耗尽并附加所有合适的元素后返回它。

    a = [100, 1, 10, 2, 3, 5, 8, 13, 21, 34, 55, 98]
    def new_list(x):
        new = []
        for item in range(len(x)):
            if x[item] < 5 and x[item] > 0:
                new.append(item)
        return new
    
    print(new_list(a))
    

【讨论】:

    【解决方案2】:

    你的 return 命令在循环内,所以一旦它通过第一种情况,它就会返回退出函数的值。

    这是您的代码应该是什么样子的示例

    a = [100, 1, 10, 2, 3, 5, 8, 13, 21, 34, 55, 98]
    
    
    def new_list(x):
        new = []
        for item in range(len(x)):            
    
            if x[item] < 5 and x[item] > 0:
                new.append(x[item])
        return new
    
    
    print new_list(a)
    

    您可以使用列表推导式获得相同的结果

    def new_list(x):
        return [item for item in x if 0 < item < 5]
    

    【讨论】:

    • 您附加的是索引,而不是列表元素。 item 是此代码中的索引。此代码输出 [1, 3, 4]4 甚至不在列表中。
    • @timgeb 你是对的。编辑以反映更正
    【解决方案3】:

    只是我的建议。您可以在此处使用filter(),而不是创建自己的循环。

    a = [100, 1, 10, 2, 3, 5, 8, 13, 21, 34, 55, 98]
    
    def new_list(x, low=0, high=5):
        return filter(lambda f: f in range(low, high), x)
    

    Filter 返回一个新列表,其中包含传递给定谓词的元素,它等效于

    [item for item in iterable if function(item)]
    

    根据文档。

    因此

    print new_list(a)
    

    结果:

    [1, 2, 3, 5]
    

    这样您可以检查任何值,例如:

    print new_list(a, 5, 10)
    [5, 8]
    

    【讨论】:

      【解决方案4】:

      您每次通过循环都将new 重置为一个全新的空列表,这会丢弃之前迭代中完成的所有工作。

      另外,在if 语句中,您调用return,它会立即退出您的函数,因此您永远不会处理列表的其余部分。

      你可能想要这样的东西:

      def new_list(x):
          new = []
          for item in x:
              if 0 < item < 5:
                  new.append(item)
          return new
      

      【讨论】:

        【解决方案5】:

        三个错误:

        1. 您将在 for 循环的每次迭代中重新实例化 new
        2. 您应该在列表构建完成后,在函数结束时return new
        3. 您正在附加item,但这是您的索引。在您的代码中,您必须附加 x[item]

        有更正的代码:

        a = [100, 1, 10, 2, 3, 5, 8, 13, 21, 34, 55, 98]
        
        def new_list(x):
            new = []
        
            for item in range(len(x)):
                if x[item] < 5 and x[item] > 0:
                    new.append(x[item])
            return new
        
        print(new_list(a))
        

        输出:

        [1, 2, 3]
        

        建议:

        1. 不要索引,直接循环x的项目(for item in x: ...)。
        2. 使用链式比较,例如0 &lt; item &lt; 5
        3. 考虑一个列表理解。

        包含所有三个建议的代码:

        >>> [item for item in a if 0 < item < 5]
        >>> [1, 2, 3]
        

        【讨论】:

          猜你喜欢
          • 2013-11-30
          • 2021-10-31
          • 2022-07-06
          • 1970-01-01
          • 1970-01-01
          • 2014-01-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多