Python 偶数一个列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4167217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:36:08  来源:igfitidea点击:

Even numbers a list?

pythonlist

提问by

How do I create a list and only extract or search out the even numbers in that list?

如何创建列表并仅提取或搜索该列表中的偶数?

Create a function even_only(l)that takes a list of integers as its only argument. The function will return a new list containing all (and only) the elements of l which are evenly divisible by 2. The original list l shall remain unchanged.

创建一个even_only(l)将整数列表作为其唯一参数的函数。该函数将返回一个包含所有(且仅)l 中可被 2 整除的元素的新列表。原始列表 l 应保持不变。

For examples, even_only([1, 3, 6, 10, 15, 21, 28])should return [6, 10, 28], and even_only([1, 4, 9, 16, 25])should return [4, 16].

例如,even_only([1, 3, 6, 10, 15, 21, 28])应该返回[6, 10, 28],并且 even_only([1, 4, 9, 16, 25])应该返回[4, 16]

Hint: Start by creating an empty list, and whenever you encounter an even number in it, add it to your list, then at the end, return your list.

提示:首先创建一个空列表,每当您遇到偶数时,将其添加到您的列表中,然后在最后返回您的列表。

采纳答案by Mark Rushakoff

"By hand":

“用手”:

def even_only(lst):
    evens = []
    for number in lst:
        if is_even(number):
            evens.append(number)
    return evens

Pythonic:

蟒蛇:

def even_only(iter):
    return [x for x in iter if is_even(x)]

Since it's homework, you can fill in the is_evenfunction.

既然是作业,就可以填is_even函数了。

回答by Donut

Simplest way would be to do what you posted in a comment -- iterate through the input list to find digits evenly divisible by 2, and add them to the return list if so.

最简单的方法是执行您在评论中发布的操作 - 遍历输入列表以查找可被 2 整除的数字,如果是,则将它们添加到返回列表中。

The list.append(x)function will help you add an item to a list.

list.append(x)功能将帮助您将项目添加到列表中。

Also as mentioned, look at using the modulooperation to determine if a number is divisible by 2...

同样如上所述,看看使用运算来确定一个数字是否可以被 2 整除......

回答by Vincent Savard

The best way to do this (as a beginner) is probably a comprehension list. Since this is a homework, I won't do it for you, but here is the syntax :

做到这一点的最好方法(作为初学者)可能是一个理解列表。由于这是一个家庭作业,我不会为你做,但这里是语法:

[x for x in your_list if (your condition)]

You just have to replace (your condition) with what fits well (basically, exactly what you described).

您只需将(您的状况)替换为合适的(基本上,正是您所描述的)。

P.S. I know some people may say comprehension lists are a bit advanced for a beginner, but I think it is not a concept too hard to catch and extremely useful.

PS 我知道有些人可能会说理解列表对于初学者来说有点高级,但我认为它不是一个很难掌握的概念,而且非常有用。

回答by Troy J. Farrell

Use the filterfunction to do this in a functional way:

使用该filter函数以函数方式执行此操作:

>>> even_filter = lambda x: not x % 2
>>> result = filter(even_filter, [0, 1, 2, 3, 4])
>>> assert result == [0, 2, 4]

Edit: updated with the correct parity of zero per Vincent's comment.

编辑:根据文森特的评论更新了正确的零奇偶校验。

回答by u478344

>>> a = [1, 3, 6, 10, 15, 21, 28]
>>> b = [i for i in a if i%2 ==0 ]
>>> b
[6, 10, 28]
>>> a
[1, 3, 6, 10, 15, 21, 28]

回答by PaulMcG

>>> even_only = lambda seq : [ x for x in seq if str(x)[-1] in "02468" ]
>>> even_only([1, 3, 6, 10, 15, 21, 28])
[6, 10, 28]

回答by Vlox

I recently had this issue and used:

我最近遇到了这个问题并使用了:

list=[1,2,3,4,5,6] #whatever your list is, just a sample
evens=[x for x in list if np.mod(x,2)==0]
print evens

returns [2,4,6]

返回 [2,4,6]

回答by Yuriy Samorodov

All = (1, 3, 6, 10, 15, 21, 28] From itertools import takewhile Evenonly = takewhile(lambda x: x%2 == 0, All) Print (list (Evenonly)

All = (1, 3, 6, 10, 15, 21, 28] from itertools import takewhile Evenonly = takewhile(lambda x: x%2 == 0, All) Print (list (Evenonly)

回答by Vivek Singh

Seems a bit late.But whats given below, works fine for me:

似乎有点晚了。但是下面给出的内容对我来说很好用:

def even_list(*args):  
    # Returns the even numbers in the list  
    return [x for x in args if (x % 2 == 0)] 



even_list(1,2,3,4,5,6,7,8)  
[2, 4, 6, 8]  

回答by Ioannis Nasios

for every element in list, if element of list modulo is 0 then number is even

对于列表中的每个元素,如果列表中的元素取模为 0,则数字为偶数

initial_list =  [1,22,13,41,15,16,87]
even_list = [ x  for x in initial_list if  x % 2 == 0]
even_list
# [22, 16]