Generator Comprehensions? was Re: a break for comprehensions

Alex Martelli aleaxit at yahoo.com
Wed Aug 1 09:18:53 EDT 2001


"Ken Seehof" <kseehof at neuralintegrator.com> wrote in message
news:mailman.996643861.24860.python-list at python.org...
    ...
> In practice, this puts a filter on an existing lazy sequence.
Unfortunately
> the filter is limited to a one-to-one mapping of the input sequence, but
> that's true of list comprehensions too isn't it?

Many-to-one, in both cases, e.g.
    [somefun(x) for x in seq if somecond(x)]

One-to-many (thus clearly also many-to-many) can be obtained for
concrete sequences with the help of another 'nested' for:

    [somefun2(x,i) for x in seq for i in range(3) if somecond2(x,i)]

may end up with 0 to 2 items of the output sequence for each
item x of the input sequence seq.


Alex






More information about the Python-list mailing list