"for" cycle with assigning index

dmitrey dmitrey.kroshko at scipy.org
Sat Aug 15 03:17:04 EDT 2009


Hi all,
could you inform me how to do it properly?

I have the cycle

for i in xrange(len(Funcs2)): # Funcs2 is Python dict
     Funcs.append(lambda *args, **kwargs: (Funcs2[i](*args, **kwargs)
[IndDict[left_arr_indexes[i]]]))

So, all the Funcs are initialized with i = last index = len(Funcs2)

When I involve

for i in xrange(len(Funcs2)):
     Funcs.append(lambda i=i,*args, **kwargs: (Funcs2[i](*args,
**kwargs)[IndDict[left_arr_indexes[i]]]))

I get "list indices must be integers, not dict" (and i is equal to
Python dictionary, that is Funcs2)

So, how to do it correctly?
Thank you in advance, D.



More information about the Python-list mailing list