list-comprehension and map question (simple)

Nicolas Évrard nicoe at altern.org
Sun Mar 27 10:19:00 EST 2005


* Charles Hartman  [16:51 27/03/05 CEST]: 
>I understand this toy example:
>
>lines = "this is a group\nof lines of\nwords"
>def getlength(w): return len(w)
>s = map(getlength, [word for ln in lines.split() for word in  
>ln.splitlines()])
>
>(now s is [4, 2, 1, 5, 2, 5, 2, 5])
>
>My question is whether there's any compact way to combine function  
>calls, like this (which doesn't work):
>
>lines = "this is a group\nof lines of\nwords"
>def getlength(w): return len(w)
>def timestwo(x): return x * 2
>s = map(timestwo(getlength), [word for ln in lines.split() for word in  
>ln.splitlines()])
>
>I hope the question is clear enough. I have a feeling I'm ignoring a  
>simple technique . . .

lambda !

map(lambda x: timestwo(getlength(x)), ...)

-- 
(°>  Nicolas Évrard
/ )  Liège - Belgique
^^



More information about the Python-list mailing list