473,378 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 1363
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
7
by: Paddy McCarthy | last post by:
Hi, I am trying to use eval as little as possible but solve this problem. #If given:two or more lambda equations x=lambda : A < B y=lambda : C+6 >= 7 .... How do I create another lambda...
17
by: mehmetmutigozel | last post by:
I was thinking about something like the following; >>> a= Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 't' is not defined >>> or
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.