Stéphane Larouche a écrit :
(snip)
>
> funcs = [(lambda i: lambda x: x+i)(i) for i in xrange(10)]
A bit more complex than necessary... The canonical solution is
funcs = [lambda x, i=i: x+i for i in xrange(10)]
--
http://mail.python.org/mailman/listinfo/python-list
Ryan Ginstrom ginstrom.com> writes:
> How about:
>
> >>> def make_adder(i):
> def adder(x):
> return x+i
> return adder
>
> >>> funcs = [make_adder(i) for i in xrange(10)]
> >>> print [func(10) for func in funcs]
> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
> >>>
Or if
[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
> On Behalf Of [EMAIL PROTECTED]
> 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.
How about:
>>> def make_adder(i):
def adder(x):
return x+i
return adder
On Wed, 19 Sep 2007 04:39:44 -0700, dmitrey.kroshko wrote:
> 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
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 val