Skip to main content
 首页 » 编程设计

python之从列表中删除奇数

2025年05月04日10haluo1

当我运行下面的代码时,我没有得到正确的答案。它在应该返回 [4, 4] 时返回 [4]。

def purify(y): 
    new_numbers = [] 
    for x in y: 
            if x%2 ==0: 
                    new_numbers.append(x) 
        return new_numbers 
 
print(purify([4,5,5,4])) 

请您参考如下方法:

您的缩进已关闭,将返回放在 for 循环运行之后

def purify(y): 
    new_numbers = [] 
    for x in y: 
            if x%2 ==0: 
                    new_numbers.append(x) 
    return new_numbers