| Store | Cart

list-comprehension and map question (simple)

From: Nicolas Évrard <nic...@altern.org>
Sun, 27 Mar 2005 17:19:00 +0200
* 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
^^

Recent Messages in this Thread
Charles Hartman Mar 27, 2005 02:51 pm
Nicolas Évrard Mar 27, 2005 03:19 pm
Brian van den Broek Mar 27, 2005 06:18 pm
Charles Hartman Mar 27, 2005 06:35 pm
Brian van den Broek Mar 27, 2005 07:12 pm
Brian van den Broek Mar 27, 2005 07:41 pm
Cameron Laird Mar 30, 2005 07:08 pm
runsun pan Mar 28, 2005 02:51 pm
Robert Kern Mar 28, 2005 03:04 pm
runsun pan Mar 30, 2005 04:32 pm
Messages in this thread