[ x for x in xrange(10) when p(x) ]

bonono at gmail.com bonono at gmail.com
Thu Nov 10 06:18:23 EST 2005


Paul Rubin wrote:
> "bonono at gmail.com" <bonono at gmail.com> writes:
> > > How does a useless generator expression make it more generic?
> >
> > xrange is only picked as an example. I may be newbie on python but not
> > that dumb if all I want is a list of integer(sorted) that meets certain
> > criteria.
> >
> > takewhile(p, (x for x in
> > some_function_that_could_potentially_generate_a_long_list_of_elements_but_first_element_that_meets_the_condition_can_come_fast(*args,**kwargs)))
>
> The generator expression is useless in that example too.  some_function...
> has to return an iterator, not a list, if you don't want it to use a
> pile of memory.  And if it returns an iterator, the generator
> expression is redundant.  You can pass the iterator directly to
> takewhile.
oops, my original code is much more complex than that.  it is not the
pythonic way of doing things but that is what I like. Try again:

takewhile(p, ((exp1(x), exp2(y)) for (x, y) in f()))




More information about the Python-list mailing list