FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


A question about Python lambda





davidv
So I've been scouring the internet for the past 3 hours trying to find a way to increment while in lambda using Python.

I wanted to find the number of occurrences of x in a string. For example,

Code:
s = 'a,b,c,d,e,f,g'


I wanted to find out how many commas there were in s. So what I did was this (which really was just creating a new list of commas and then returning the length of that new list i.e. number of commas in the string):

Code:
>>> f = lambda x: len([i for i in x if i is ','])
>>> s = 'a,b,c,d,e,f,g'
>>> f(s)
6


Thing is, I wanted to increment a counter each time the conditional expression was true but I kept getting a syntax error which lead to one conclusion. I don't know very much about lambda and list comprehensions in Python.

Code:
>>> f = lambda x, n = 0: [n += 1 for i in x if i is ',']
SyntaxError: invalid syntax
>>>


Can anyone explain to me why that's incorrect?
jcreus
davidv wrote:
So I've been scouring the internet for the past 3 hours trying to find a way to increment while in lambda using Python.

I wanted to find the number of occurrences of x in a string. For example,

Code:
s = 'a,b,c,d,e,f,g'


I wanted to find out how many commas there were in s. So what I did was this (which really was just creating a new list of commas and then returning the length of that new list i.e. number of commas in the string):

Code:
>>> f = lambda x: len([i for i in x if i is ','])
>>> s = 'a,b,c,d,e,f,g'
>>> f(s)
6


Thing is, I wanted to increment a counter each time the conditional expression was true but I kept getting a syntax error which lead to one conclusion. I don't know very much about lambda and list comprehensions in Python.

Code:
>>> f = lambda x, n = 0: [n += 1 for i in x if i is ',']
SyntaxError: invalid syntax
>>>


Can anyone explain to me why that's incorrect?

I believe that it's because you have the assignation operator which is not allowed. A workaround would be creating a function which increments a global variable named n. Or else, this code would do exactly the same, to know the number of times it is repeated:

Code:

times = len(s.split(","))-1


The code hasn't been tested though.

Hope it helps.
davidv
jcreus wrote:
davidv wrote:
So I've been scouring the internet for the past 3 hours trying to find a way to increment while in lambda using Python.

I wanted to find the number of occurrences of x in a string. For example,

Code:
s = 'a,b,c,d,e,f,g'


I wanted to find out how many commas there were in s. So what I did was this (which really was just creating a new list of commas and then returning the length of that new list i.e. number of commas in the string):

Code:
>>> f = lambda x: len([i for i in x if i is ','])
>>> s = 'a,b,c,d,e,f,g'
>>> f(s)
6


Thing is, I wanted to increment a counter each time the conditional expression was true but I kept getting a syntax error which lead to one conclusion. I don't know very much about lambda and list comprehensions in Python.

Code:
>>> f = lambda x, n = 0: [n += 1 for i in x if i is ',']
SyntaxError: invalid syntax
>>>


Can anyone explain to me why that's incorrect?

I believe that it's because you have the assignation operator which is not allowed. A workaround would be creating a function which increments a global variable named n. Or else, this code would do exactly the same, to know the number of times it is repeated:

Code:

times = len(s.split(","))-1


The code hasn't been tested though.

Hope it helps.


If assignment operators aren't allowed then I can see why it wouldn't work. Yeah, a global variable would work but my script has gotten white large over the past few days and I don't want to use globals. Yes, that line you wrote should work too but so will a million other ways. I just wanted to practice using lambda. Thanks for the reply Smile
cgkanchi
Just to expand on this, assignment operators aren't allowed in either lambdas or list comprehensions.

In addition, while lambdas are awesome, they're a tool to be used carefully. Guido has slowly been pushing the functional programming features into the background in newer versions of Python. While lambdas will continue to exist, the intention is to replace many of the more common uses of lambdas (such as in combination with map() and filter()) with list comprehensions instead.

Cheers,
cgkanchi
Related topics
php or python question about some processing.
python
Does frihost still support Python for CGI programming
Python Support ???
Python, CGI and CSS
Variant of Python on servers?
Question about python
A question about dictionary of lists -- Python
Python
Python - Using files
Python random password generator in one line
lambda functions
Python Sequences: Getting length, min, max
Tutorial needed for python cgi
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.