使用列表推导在偶数索引处的偶数列表 [英] List of even numbers at even number indexes using list comprehension
问题描述
我正在尝试使用列表推导在偶数索引位置生成偶数列表. else值将为0
.我已经完成了与以下功能相同的功能
def even_list(l):
ll = []
for x in l:
if x%2 == 0:
ll.append(x)
ll.append(0)
return ll[:-1]
现在,我需要将其转变为列表理解.
input = [11, 17, 12, 17, 40, 19, 12, 16]
out = [12, 0, 40, 0, 12, 0, 16]
这是我尝试过的(输入l
而lo
退出):
>>> l = [1, 2, 3, 4, 5, 6, 7]
>>> lo = []
>>> lo = [l[x] if l[x]%2 == 0 and len(lo)%2 == 0 else 0 for x in range(len(l))]
>>> print lo
[0, 2, 0, 4, 0, 6, 0]
我无法从您的帖子中确切地找到您想要的东西,但是我想这就是您想要的:
给出一个列表,得到所有数字在偶数索引处.如果这些数字中的任何一个都为偶数,则将它们放在新列表中并返回:
In [10]: L = [3,1,54,5,2,3,4,5,6,5,2,5,3,2,5,2,2,5,2,5,2]
In [11]: [num for i,num in enumerate(L) if not num%2 and not i%2]
Out[11]: [54, 2, 4, 6, 2, 2, 2, 2]
如果要在两者之间添加0
,则可以做一些itertools
魔术:
In [12]: list(itertools.chain.from_iterable(zip((num for i,num in enumerate(L) if not num%2 and not i%2), itertools.cycle([0]))))[:-1]
Out[12]: [54, 0, 2, 0, 4, 0, 6, 0, 2, 0, 2, 0, 2, 0, 2]
好吧,那是很多括号和括号,所以让我们仔细看一下:
-
list(...)[:-1]
将...
转换为list
,并获取除该列表的最后一个元素以外的所有元素.这类似于您添加0
s并删除最后一个 时尝试执行的操作
-
(num for i,num in enumerate(L) if not num%2 and not i%2)
与编辑之前的内容相同,只是它使用括号(()
)而不是括号([]
).与列表理解相反,这变成了生成器理解-仅因为它执行了一点优化就很重要-直到需要它们时才计算值(直到zip
要求下一个值) -
itertools.cycle([0])
给出了无限的0
s 列表
-
zip(A, B)
返回一个元组列表,其中第i
个元组具有两个元素-A
的第i
个元素和B
i元素/p> -
itertools.chain.from_iterable(zip(A, B))
返回交错的A
和B
的元素,作为生成器.从本质上讲,就像这样做:
def someFunc(A, B): for i in range(len(A)): yield A[i] yield B[i]
因此,所有这些结合在一起就可以为您提供您想要的一切
I am trying to generate an even numbered list at even index locations using a list comprehension. The else values will be 0
. I have done the function which does the same as below
def even_list(l):
ll = []
for x in l:
if x%2 == 0:
ll.append(x)
ll.append(0)
return ll[:-1]
Now I need to turn this into a list comprehension.
input = [11, 17, 12, 17, 40, 19, 12, 16]
out = [12, 0, 40, 0, 12, 0, 16]
This is what I have tried (l
is input and lo
is out):
>>> l = [1, 2, 3, 4, 5, 6, 7]
>>> lo = []
>>> lo = [l[x] if l[x]%2 == 0 and len(lo)%2 == 0 else 0 for x in range(len(l))]
>>> print lo
[0, 2, 0, 4, 0, 6, 0]
I wasn't able to figure out exactly what you were looking for from your post, but here's what I think you want:
Given a list, get all the numbers at even indices. If any of these numbers are even, put them in a new list and return it:
In [10]: L = [3,1,54,5,2,3,4,5,6,5,2,5,3,2,5,2,2,5,2,5,2]
In [11]: [num for i,num in enumerate(L) if not num%2 and not i%2]
Out[11]: [54, 2, 4, 6, 2, 2, 2, 2]
If you want to add 0
s in between, then you can do a little itertools
magic:
In [12]: list(itertools.chain.from_iterable(zip((num for i,num in enumerate(L) if not num%2 and not i%2), itertools.cycle([0]))))[:-1]
Out[12]: [54, 0, 2, 0, 4, 0, 6, 0, 2, 0, 2, 0, 2, 0, 2]
Ok, that was a lot of brackets and parentheses, so let's take a closer look at it:
list(...)[:-1]
converts...
into alist
and gets all but the last element of that list. This is similar to what you were trying to do when you added0
s and removed the last one(num for i,num in enumerate(L) if not num%2 and not i%2)
is the same as what it was before the edit, except that it uses parentheses (()
) instead of brackets ([]
). This turns it into a generator-comprehension, as opposed to a list comprehension - it only matters in that it performs a little bit of optimization - the values are not computed until they are needed (untilzip
asks for the next value)itertools.cycle([0])
gives an endless list of0
szip(A, B)
returns a list of tuples, in which thei
th tuple has two elements - thei
th element ofA
, and thei
th element ofB
itertools.chain.from_iterable(zip(A, B))
returns the elements ofA
andB
interleaved, as a generator. In essence, it's like doing this:
def someFunc(A, B): for i in range(len(A)): yield A[i] yield B[i]
Thus, all of these put together give you exactly what you want
这篇关于使用列表推导在偶数索引处的偶数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!