Re: Two questions on lambda:

2005-06-24 Thread Christophe Delord
hello,

On Fri, 24 Jun 2005 14:48:16 +0200, Xavier Décoret wrote:

> Hi,
> 
> In the same spirit, how can I do to compute intermediary values in the
> 
> body of a lambda function. Let's say (dummy example):
> 
> f = lambda x : y=x*x,y+y
> 
> 
> In languages like Caml, you can do:
> 
> let f = function x -> let y=x*x in y+y;;
> 
> Does the lambda : syntax in python allow for the same kind of
> constructs?

You can define another lambda function with a default value for the y
parameter. For instance:

f = lambda x: (lambda y=x*x: y+y)()


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


Re: what list comprehension can't

2005-06-24 Thread Christophe Delord
Hello,

On 24 Jun 2005 11:45:14 -0700, [EMAIL PROTECTED] wrote:

> Hello,
> 
> Can we impose if then else into list comprehension ?
> Like we do in lambda-map form:
> 
> This code change None into 0
> L = [None, 12]
> R = map(lambda x: (x==None and [0] or x)[0], L) # [0,12]
> 

Do you mean:
   [(x==None and [0] or [x])[0] for x in L]
or [{None:0}.get(x,x) for x in L]
or [x or 0 for x in L]

Well, the third solution doesn't exactly fit the specification but may
be easier to read.


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


Re: set & random.choice question

2005-12-14 Thread Christophe Delord
Hello,

On 14 Dec 2005 12:16:22 -0800, [EMAIL PROTECTED] wrote:

> I want to do something like this:
> 
>   from random import choice
>   x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
>   somebody = random.choice(x)
> 
> but I bet a "TypeError: unindexable object" error. Any suggestions for
> an elegant workaround?

What about somebody = random.choice(list(x)) ?


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