[EMAIL PROTECTED] 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)]

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to