使用列表推导在偶数索引处的偶数列表 [英] List of even numbers at even number indexes using list comprehension

查看:135
本文介绍了使用列表推导在偶数索引处的偶数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用列表推导在偶数索引位置生成偶数列表. 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]

这是我尝试过的(输入llo退出):

>>> 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]

好吧,那是很多括号和括号,所以让我们仔细看一下:

  1. list(...)[:-1]...转换为list,并获取除该列表的最后一个元素以外的所有元素.这类似于您添加0 s并删除最后一个

  2. 时尝试执行的操作
  3. (num for i,num in enumerate(L) if not num%2 and not i%2)与编辑之前的内容相同,只是它使用括号(())而不是括号([]).与列表理解相反,这变成了生成器理解-仅因为它执行了一点优化就很重要-直到需要它们时才计算值(直到zip要求下一个值)

  4. itertools.cycle([0])给出了无限的0 s

  5. 列表
  6. zip(A, B)返回一个元组列表,其中第i个元组具有两个元素-A的第i个元素和B i元素/p>

  7. itertools.chain.from_iterable(zip(A, B))返回交错的AB的元素,作为生成器.从本质上讲,就像这样做:


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 0s 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:

  1. list(...)[:-1] converts ... into a list and gets all but the last element of that list. This is similar to what you were trying to do when you added 0s and removed the last one

  2. (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 (until zip asks for the next value)

  3. itertools.cycle([0]) gives an endless list of 0s

  4. zip(A, B) returns a list of tuples, in which the ith tuple has two elements - the ith element of A, and the ith element of B

  5. itertools.chain.from_iterable(zip(A, B)) returns the elements of A and B 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆