Extended List Comprehension

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Stroud

    #1

    Extended List Comprehension

    Would anyone else find this syntax useful for generator expressions?

    py> [x for x in '1234' if x%2 else 'even']
    [1, 'even', 3, 'even']

    I'm guessing this has been suggested before?

    James
  • Paul Rubin

    #2
    Re: Extended List Comprehension

    James Stroud <jstroud@ucla.e du> writes:[color=blue]
    > py> [x for x in '1234' if x%2 else 'even']
    > [1, 'even', 3, 'even']
    >
    > I'm guessing this has been suggested before?[/color]

    You could (in 2.5) use:

    [(x if x%2 else 'even') for x in '1234']

    Comment

    • James Stroud

      #3
      Re: Extended List Comprehension

      James Stroud wrote:[color=blue]
      > Would anyone else find this syntax useful for generator expressions?
      >
      > py> [x for x in '1234' if x%2 else 'even']
      > [1, 'even', 3, 'even']
      >
      > I'm guessing this has been suggested before?
      >
      > James[/color]

      Before anyone notices, I should say:

      [x for x in [1,2,3,4] if x%2 else 'even']

      Darn'ed imaginary interpreters!

      Comment

      • Tim Chase

        #4
        Re: Extended List Comprehension

        >>py> [x for x in '1234' if x%2 else 'even'][color=blue][color=green]
        >>[1, 'even', 3, 'even']
        >>
        >>I'm guessing this has been suggested before?[/color]
        >
        >
        > You could (in 2.5) use:
        >
        > [(x if x%2 else 'even') for x in '1234'][/color]

        This failed on multiple levels in 2.3.5 (the "if" syntax is
        unrecognized, and trying the below with '1234' rather than
        [1,2,3,4] choked) Haven't played with 2.5 yet, so this might be
        a good way to go if you've got it.

        However, this worked in my 2.3.5:

        [((x%2 == 0 and 'even') or x) for x in [-2,-1,0,1,2,3,4]]

        It uses the fairly defacto short-circuit-evaluation usage to coin
        a Python trinary operator like the C/C++ colon/question-mark pair
        of operators.

        There is a caveat (that can be coded around, IIRC, but don't have
        it at the tip of my fingers) that inverting the logic can cause
        trouble when x==0 as in

        [((x%2 == 0 and x) or 'odd') for x in [-2,-1,0,1,2,3,4]]

        which doesn't quite produce the desired results.

        -tim@justonemore tim.com










        Comment

        • Ido Yehieli

          #5
          Re: Extended List Comprehension

          As Paul already demonstrated, this is hardly needed since it can be
          done more clearly with existing lang. features.

          Comment

          • Joel Bender

            #6
            Re: Extended List Comprehension

            > You could (in 2.5) use:[color=blue]
            >
            > [(x if x%2 else 'even') for x in '1234'][/color]

            Or this:

            [int(x)&1 and x or 'even' for x in '1234']

            Comment

            • Paul Rubin

              #7
              Re: Extended List Comprehension

              Tim Chase <python.list@ti m.thechases.com > writes:[color=blue][color=green]
              > > You could (in 2.5) use:
              > > [(x if x%2 else 'even') for x in '1234'][/color]
              >
              > This failed on multiple levels in 2.3.5 (the "if" syntax is
              > unrecognized,[/color]

              Yes, that syntax is new to 2.5.
              [color=blue]
              > However, this worked in my 2.3.5:
              > [((x%2 == 0 and 'even') or x) for x in [-2,-1,0,1,2,3,4]]
              > It uses the fairly defacto short-circuit-evaluation usage to coin a
              > Python trinary operator like the C/C++ colon/question-mark pair of
              > operators.[/color]

              Yeah, you could do that, but there's various drawbacks, hence the new
              syntax being introduced in 2.5.

              Comment

              Working...