"for" cycle with assigning index

Paul Rubin http
Sat Aug 15 10:15:13 CEST 2009


Carl Banks <pavlovevidence at gmail.com> writes:
> def create_funcs_caller(i):
>     def func(*args,**kwargs):
>         return(Funcs2[i](*args,**kwargs)[IndDict[left_arr_indexes[i]]])
>     retirm func
> 
> for i in xrange(len(Funcs2)):
>     Funcs.append(create_funcs_caller(i))

I prefer to get rid of the index variable:

    def create_funcs_caller(f,ix):
       return lambda *args, **kw: f(*args,**kw)[ix]

    Funcs = list(create_funcs_caller(f,ix)
                   for f,ix in zip(Funcs2, left_arr_indexes))

Or in point-free style:

    from itertools import starmap

    Funcs = starmap(create_funcs_caller, zip(Funcs2,left_arr_indexes))



More information about the Python-list mailing list