FAQ
Is list comprehension on its way into the language? Or is that just a
rumour, based on Greg's patch?

And - as allways, I can't leave the syntax alone without a suggestion
of my own... Greg's patch works like this (among other things):

[(i, s) for i in nums for s in strs]

To me, this sounds a bit like

[(i, s) for i in [nums for s in strs]]

which I guess doesn't give much meaning. I think the syntax would be
clearer (and more natural language-like) with an "and" instead of the
"for"s, after the first one. The first ones marks that this is the
list of (i, s) for all of the "i"s and all of the "s"s given. Thus:

[(i, s) for i in nums and s in strs]

--

Magnus Echelon jamming noise:
Lie FBI CIA NSA Handgun Assault Bomb Drug Terrorism
Hetland Special Forces Delta Force AK47 Hillary Clinton

Search Discussions

  • William Tanksley at Dec 13, 1999 at 1:25 am

    On 13 Dec 1999 01:39:12 +0100, Magnus L. Hetland wrote:
    Is list comprehension on its way into the language? Or is that just a
    rumour, based on Greg's patch?
    I hope it's going it.
    And - as allways, I can't leave the syntax alone without a suggestion
    of my own... Greg's patch works like this (among other things):
    The amusing thing is that I tried to do the exact same thing as you're
    about to try.
    [(i, s) for i in nums for s in strs]
    To me, this sounds a bit like
    [(i, s) for i in [nums for s in strs]]
    which I guess doesn't give much meaning. I think the syntax would be
    clearer (and more natural language-like) with an "and" instead of the
    "for"s, after the first one. The first ones marks that this is the
    list of (i, s) for all of the "i"s and all of the "s"s given. Thus:
    [(i, s) for i in nums and s in strs]
    First of all, notice that "nums and s" is a valid parse of the middle of
    this string. Thus, it's ambiguous.

    Second, even if it wasn't, you forgot that 'if' is a valid keyword in such
    a comprehension.
    Magnus
    --
    -William "Billy" Tanksley, in hoc signo hack
  • Greg Ewing at Dec 13, 1999 at 1:27 pm

    "Magnus L. Hetland" wrote:
    [(i, s) for i in nums and s in strs]
    That would be ambiguous, because it could be parsed as

    [(i, s) for i in (nums and s in strs)]

    It *might* be feasible to use

    [(i, s) for i in nums and for s in strs]

    If I get a spare moment or two one day I'll try this
    and see whether the parser chokes. (Python's parser
    is a bit strange - it's hard to predict what it can
    handle and what it can't.)

    Greg
  • Magnus L. Hetland at Dec 13, 1999 at 10:29 pm

    Greg Ewing <greg.ewing at compaq.com> writes:

    "Magnus L. Hetland" wrote:
    [(i, s) for i in nums and s in strs]
    That would be ambiguous, because it could be parsed as

    [(i, s) for i in (nums and s in strs)]
    Which, in the case that nums is not an empty list, would be

    [(i, s) for i in nums]

    IC. Oh, well... But what about:

    [(i, s) for i in nums, and s in strs]

    This is still grammatically correct English, AFAIK... And the ", and"
    constellation does not have another valid interpretation, does it?

    (Though it might not be really pretty...)
    It *might* be feasible to use

    [(i, s) for i in nums and for s in strs]
    Nice. Much less confusing than just using the *for* directly.

    (Of course, any mechanisms of parallel iteration might be used here if
    they appear... Like [(i, s) for i, s in nums, strs] or something)
    If I get a spare moment or two one day I'll try this
    and see whether the parser chokes. (Python's parser
    is a bit strange - it's hard to predict what it can
    handle and what it can't.) Good :)
    Greg
    --

    Magnus Echelon jamming noise:
    Lie FBI CIA NSA Handgun Assault Bomb Drug Terrorism
    Hetland Special Forces Delta Force AK47 Hillary Clinton

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 13, '99 at 12:39a
activeDec 13, '99 at 10:29p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase