471,337 Members | 851 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

lambda-funcs problem

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?
Evaluation speed is also very important to me.

Thank you in advance, D.

Sep 19 '07 #1
5 1309
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 yields x+
(N-1) for any func.

So what's the best way to make it valid?
The variable is bound to the name `i` when the lambda function is
created not to the value that `i` had at that time. The idiomatic way is
to use a default value for an argument because those are evaluated at
definition time of functions::

F.append(lambda x, i=i: x + i)

Ciao,
Marc 'BlackJack' Rintsch
Sep 19 '07 #2
dm*************@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)]

Sep 19 '07 #3
On Behalf Of dm*************@scipy.org
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
>>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]
>>>
Regards,
Ryan Ginstrom

Sep 19 '07 #4
Ryan Ginstrom <software <atginstrom.comwrites:
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 you want a one liner:

funcs = [(lambda i: lambda x: x+i)(i) for i in xrange(10)]

Stéphane

Sep 19 '07 #5
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)]
Sep 19 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

63 posts views Thread by Stephen Thorne | last post: by
26 posts views Thread by Steven Bethard | last post: by
7 posts views Thread by Paddy McCarthy | last post: by
17 posts views Thread by mehmetmutigozel | last post: by
6 posts views Thread by jena | last post: by
4 posts views Thread by Xah Lee | last post: by
5 posts views Thread by Octal | last post: by
21 posts views Thread by globalrev | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.