Python:如何循环列表并附加到新列表

Python:如何循环列表并附加到新列表,python,list,for-loop,append,Python,List,For Loop,Append,练习我的python 任务: 循环浏览列表A并创建一个新列表,其中仅包含列表A中介于0-5之间的项目 我做错了什么 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

练习我的python

任务: 循环浏览列表A并创建一个新列表,其中仅包含列表A中介于0-5之间的项目

我做错了什么

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))
a=[100,1,10,2,3,5,8,13,21,34,55,98]
def新_列表(x):
对于范围(len(x))中的项目:
新=[]
如果x[项目]<5且x[项目]>0:
(新增.附加(项目))
还新
打印(新列表(a))

我得到的答案是
[1]

您返回的命令在循环中,因此一旦它通过第一个案例,它就会返回退出函数的值

下面是一个代码应该是什么样子的示例

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)
a=[100,1,10,2,3,5,8,13,21,34,55,98]
def新_列表(x):
新=[]
对于范围(len(x))中的项目:
如果x[项目]<5且x[项目]>0:
新增。追加(x[项目])
还新
打印新列表(a)
您可以通过使用列表来获得相同的结果

def new_list(x):
    return [item for item in x if 0 < item < 5]
def新列表(x):
返回[如果0<项目<5,则x中项目对应项目]

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

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

你可能想要这样的东西:

def new_list(x):
    new = []
    for item in x:
        if 0 < item < 5:
            new.append(item)
    return new
def新列表(x):
新=[]
对于x中的项目:
如果0<项目<5:
新增。追加(项目)
还新
三个错误:

  • 您正在为循环的每次迭代重新初始化
    new
  • 在函数结束时,当列表构建完成时,您应该
    返回new
  • 您正在追加
    ,但这是您的索引。在代码中,必须附加
    x[项目]
  • 带更正的代码:

    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))
    
    建议:

    >>> [item for item in a if 0 < item < 5]
    >>> [1, 2, 3]
    
  • 不编制索引,直接在
    x
    的项目上循环(
    对于x中的项目:…
  • 使用链式比较,例如
    0
  • 考虑一个列表
  • 代码包含所有三个建议:

    >>> [item for item in a if 0 < item < 5]
    >>> [1, 2, 3]
    
    >>>[如果0>> [1, 2, 3]
    
    这正是我的建议。您可以使用此处,而不是创建自己的循环

    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)
    
    过滤器返回一个新的列表,其中的元素传递一个给定的谓词,该谓词等价于

    [item for item in iterable if function(item)]
    
    根据文件

    因此

    print new_list(a)
    
    结果为:

    [1, 2, 3, 5]
    
    通过这种方式,您可以检查任何值,例如:

    print new_list(a, 5, 10)
    [5, 8]
    
    只是一个建议

  • 空列表位于For循环的内,这意味着每次迭代都会创建一个新的空列表

  • “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))
    
    a=[100,1,10,2,3,5,8,13,21,34,55,98]
    def新_列表(x):
    新=[]
    对于范围(len(x))中的项目:
    如果x[项目]<5且x[项目]>0:
    新增。追加(项目)
    还新
    打印(新列表(a))
    

  • new=[]
    移动到循环之前,以便添加索引,而不是列表元素<代码>项目
    是此代码中的索引。此代码输出
    [1,3,4]
    ,但
    4
    不在列表中。@timgeb您是对的。编辑以反映更正