Fast recursive generators?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Oct 28 23:18:04 EDT 2011


En 2011年10月28日 15:10:14 -0300, Michael McGlothlin 
<michaelm at plumbersstock.com> escribió:
> I'm trying to generate a list of values where each value is dependent
> on the previous value in the list and this bit of code needs to be
> repeatedly so I'd like it to be fast. It doesn't seem that
> comprehensions will work as each pass needs to take the result of the
> previous pass as it's argument. map() doesn't seem likely. filter() or
> reduce() seem workable but not very clean. Is there a good way to do
> this? About the best I can get is this:
>> l = [ func ( start ) ]
> f = lambda a: func ( l[-1] ) or a
> filter ( f, range ( big_number, -1, -1 ) )
>>> I guess I'm looking for something more like:
>> l = do ( lambda a: func ( a ), big_number, start )

What about a generator function?
def my_generator():
 prev = 1
 yield prev
 while True:
 this = 2*prev
 yield this
 prev = this
print list(itertools.islice(my_generator(), 10))
-- 
Gabriel Genellina


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /