Function application optimization.
Bengt Richter
bokr at oz.net
Fri Dec 12 05:47:39 EST 2003
On 12 Dec 2003 10:47:50 +0100, Jacek Generowicz <jacek.generowicz at cern.ch> wrote:
>Given
>
> fncs = [func1, func2, ..., funcN]
> args = [arg1, arg2, ..., argN]
>
>How should one spell
>
> results = map(lambda f,a: f(a), fncs, args)
>
>in order to get the result most quickly ?
>
>
>
>Unfortunately "apply" takes a tuple of arguments, and there is no
>"funcall"[*] in Python.
>
>
>[*] def funcall(fn, *args):
> return fn(*args)
>
>>> fncs = [lambda x,f='func_%s(%%s)'%i:f%x for i in xrange(4)]
>>> args = 'zero one two three'.split()
>>> map(lambda f,a: f(a), fncs, args)
['func_0(zero)', 'func_1(one)', 'func_2(two)', 'func_3(three)']
I'd probably try a list comprehension
>>> [f(a) for f,a in zip(fncs,args)]
['func_0(zero)', 'func_1(one)', 'func_2(two)', 'func_3(three)']
Regards,
Bengt Richter
More information about the Python-list
mailing list