Fast recursive generators?

Discussion in 'Python' started by Michael McGlothlin, Oct 28, 2011.

  1. 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 )


    Thanks,
    Michael McGlothlin
     
    Michael McGlothlin, Oct 28, 2011
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rhino
    Replies:
    4
    Views:
    6,606
    Roedy Green
    Jan 13, 2006
  2. Replies:
    0
    Views:
    852
  3. Michele Simionato

    Python is darn fast (was: How fast is Python)

    Michele Simionato, Aug 23, 2003, in forum: Python
    Replies:
    13
    Views:
    811
  4. Carlos Ribeiro

    Style question on recursive generators

    Carlos Ribeiro, Oct 18, 2004, in forum: Python
    Replies:
    22
    Views:
    752
    Terry Reedy
    Oct 20, 2004
  5. Talin
    Replies:
    6
    Views:
    870
    Tim Peters
    Oct 31, 2005
  6. Juha Nieminen
    Replies:
    22
    Views:
    1,308
    Kai-Uwe Bux
    Oct 12, 2007
  7. n00m
    Replies:
    12
    Views:
    1,302
  8. vamsi
    Replies:
    21
    Views:
    2,409
    Keith Thompson
    Mar 9, 2009
Loading...