Reverse of map()?

Discussion in 'Python' started by Phillip Sitbon, Feb 6, 2006.

  1. Hello there,

    I have a situation where a list of functions need to be called with a
    single set of parameters and the result constructed into a tuple. I
    know there's simple ways to do it via list comprehension:

    Result = tuple( [ fn(* Args, ** Kwds) for fn in fn_list ] )

    I'd hope there's a more efficient way to do this with a built-in
    function, so that I could call:

    Result = rmap( fn_list, * Args, ** Kwds )

    and have it constructed as a tuple from the get-go.

    Is there a built-in function that would allow me to do this, or do I
    have to go with the list comprehension?
     
    Phillip Sitbon, Feb 6, 2006
    #1
    1. Advertising

  2. Phillip Sitbon <> wrote:

    > Hello there,
    >
    > I have a situation where a list of functions need to be called with a
    > single set of parameters and the result constructed into a tuple. I
    > know there's simple ways to do it via list comprehension:
    >
    > Result = tuple( [ fn(* Args, ** Kwds) for fn in fn_list ] )
    >
    > I'd hope there's a more efficient way to do this with a built-in
    > function, so that I could call:
    >
    > Result = rmap( fn_list, * Args, ** Kwds )
    >
    > and have it constructed as a tuple from the get-go.
    >
    > Is there a built-in function that would allow me to do this, or do I
    > have to go with the list comprehension?


    A genexp is probably going to be more efficient than the list
    comprehension: just omit the brackets in your first snippet.

    map(apply, fn_list, ...) may work, but I doubt it's going to be either
    simple or speedy since the ... must be replaced with as many copies of
    Args and Kwds as there are functions in fn_list, e.g.:

    map(apply, fn_list, len(fn_list)*(Args,), len(fn_list)*(Kwds))


    There's no built-in that calls many functions with identical args and
    kwds, since it's a rare need. Also, map returns a list, not a tuple, so
    it's bordering on the absurd to think that your dreamed-for rmap would
    be designed to return a tuple rather than a list -- you still have to
    call tuple on the result, any way you build it.


    Alex
     
    Alex Martelli, Feb 6, 2006
    #2
    1. Advertising

  3. [Alex Martelli]
    > map(apply, fn_list, ...) may work, but I doubt it's going to be either
    > simple or speedy since the ... must be replaced with as many copies of
    > Args and Kwds as there are functions in fn_list, e.g.:
    >
    > map(apply, fn_list, len(fn_list)*(Args,), len(fn_list)*(Kwds))


    The repeat() function in the itertools module was designed to fulfill
    this need and consume less memory in the process:

    from itertools import repeat
    n = len(fn_list)
    map(apply, fn_list, repeat(Args, n), repeat(Kwds, n))



    > There's no built-in that calls many functions with identical args and
    > kwds, since it's a rare need.


    Also, it is almost certain that the function calls and their execution
    will dominate his runtime. Even if the fantasied rmap() function
    existed, it would be unlikely to help.


    Raymond
     
    Raymond Hettinger, Feb 6, 2006
    #3
    1. Advertising

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. alex
    Replies:
    1
    Views:
    678
    Lau Lei Cheong
    Feb 4, 2005
  2. dogbite
    Replies:
    4
    Views:
    714
    osmium
    Oct 10, 2003
  3. Matthias Hildebrand
    Replies:
    5
    Views:
    8,121
    krogers
    Mar 20, 2012
  4. Vlad
    Replies:
    0
    Views:
    383
  5. Replies:
    3
    Views:
    1,022
    Andrew Koenig
    Apr 7, 2008
Loading...

Share This Page