Can I do this with list comprehension?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barberomarcelo@gmail.com

    #1

    Can I do this with list comprehension?

    Let's say I have two lists:

    a = [0, 1, 0, 1, 1, 0]
    b = [2, 4, 6, 8, 10, 12]

    I want a list comprehension that has the elements in b where a[element]
    == 1.

    That's to say, in the example above, the result must be: [4, 8, 10]

    Any hints?

    Marcelo

  • Erik Max Francis

    #2
    Re: Can I do this with list comprehension?

    barberomarcelo@ gmail.com wrote:
    Let's say I have two lists:
    >
    a = [0, 1, 0, 1, 1, 0]
    b = [2, 4, 6, 8, 10, 12]
    >
    I want a list comprehension that has the elements in b where a[element]
    == 1.
    >
    That's to say, in the example above, the result must be: [4, 8, 10]
    >
    Any hints?
    >>from itertools import izip
    >>a = [0, 1, 0, 1, 1, 0]
    >>b = [2, 4, 6, 8, 10, 12]
    >>[y for x, y in izip(a, b) if x == 1]
    [4, 8, 10]


    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Man is a hating rather than a loving animal.
    -- Rebecca West

    Comment

    • Cliff Wells

      #3
      Re: Can I do this with list comprehension?

      On Tue, 2006-08-22 at 21:07 -0700, barberomarcelo@ gmail.com wrote:
      b = [2, 4, 6, 8, 10, 12]
      >>a = [0, 1, 0, 1, 1, 0]
      >>b = [2, 4, 6, 8, 10, 12]
      >>[ j for i, j in zip ( a, b ) if i ]
      [4, 8, 10]


      --

      Comment

      • Gabriel Genellina

        #4
        Re: Can I do this with list comprehension?

        At Wednesday 23/8/2006 01:07, barberomarcelo@ gmail.com wrote:
        >a = [0, 1, 0, 1, 1, 0]
        >b = [2, 4, 6, 8, 10, 12]
        >
        >I want a list comprehension that has the elements in b where a[element]
        >== 1.
        >
        >That's to say, in the example above, the result must be: [4, 8, 10]
        print [belem for aelem,belem in zip(a,b) if aelem==1]
        print [belem for i,belem in enumerate(b) if a[i]==1]

        Or itertools.izip. ..


        Gabriel Genellina
        Softlab SRL





        _______________ _______________ _______________ _____
        Preguntá. Respondé. Descubrí.
        Todo lo que querías saber, y lo que ni imaginabas,
        está en Yahoo! Respuestas (Beta).
        ¡Probalo ya!


        Comment

        • Ben Finney

          #5
          Re: Can I do this with list comprehension?

          barberomarcelo@ gmail.com writes:
          Let's say I have two lists:
          >
          a = [0, 1, 0, 1, 1, 0]
          b = [2, 4, 6, 8, 10, 12]
          >
          I want a list comprehension that has the elements in b where a[element]
          == 1.
          >
          That's to say, in the example above, the result must be: [4, 8, 10]
          >
          Any hints?
          What have you already tried, and how is it insufficient?

          --
          \ "Unix is an operating system, OS/2 is half an operating system, |
          `\ Windows is a shell, and DOS is a boot partition virus." -- |
          _o__) Peter H. Coffin |
          Ben Finney

          Comment

          • barberomarcelo@gmail.com

            #6
            Re: Can I do this with list comprehension?

            Thanks a lot to everybody.

            Marcelo

            Comment

            Working...