【发布时间】:2014-10-03 06:34:59
【问题描述】:
我有一个测量文本长度的功能:
def length(text, option='char'):
if option == 'char':
return len(text) - text.count(' ')
elif option == 'token':
return text.count(' ') + 1
我可以得到字符文本长度:
texts = ['this is good', 'foo bar sentence', 'hello world']
text_lens = map(length, texts)
print text_lens
但是当我使用map时,如何在函数中指定第二个参数呢?
以下代码:
texts = ['this is good', 'foo bar sentence', 'hello world']
text_lens = map(length(option='token'), texts)
print text_lens
给出这个错误:
TypeError: length() takes at least 1 argument (1 given)
【问题讨论】:
标签: python list python-2.7 map typeerror