lambda-funcs problem
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Sep 19 07:58:45 EDT 2007
dmitrey.kroshko at scipy.org a écrit :
> hi all,
> I need to create a Python list of lambda-funcs that are dependent on
> the number of the ones, for example
>
> F = []
> for i in xrange(N):
> F.append(lambda x: x + i)
>
> however, the example don't work - since i in end is N-1 it yields x+
> (N-1) for any func.
>
> So what's the best way to make it valid?
It's a FAQ. The answer is (using list-comp instead of a for loop):
funcs = [lambda x, i=i : x + i for i in xrange(n)]
More information about the Python-list
mailing list