list-comprehension and map question (simple)
Charles Hartman
charles.hartman at conncoll.edu
Sun Mar 27 09:51:54 EST 2005
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()])
(Under the WingIDE I get this traceback:
"/Applications/WingIDE-Professional-2.0.2/WingIDE.app/Contents/MacOS/
src/debug/server/_sandbox.py", line 1, in ?
# Used internally for debug sandbox under external interpreter
File
"/Applications/WingIDE-Professional-2.0.2/WingIDE.app/Contents/MacOS/
src/debug/server/_sandbox.py", line 1, in addone
# Used internally for debug sandbox under external interpreter
TypeError: unsupported operand type(s) for +: 'function' and 'int'
)
I hope the question is clear enough. I have a feeling I'm ignoring a
simple technique . . .
Charles Hartman
More information about the Python-list
mailing list