Howegrown wordcount
Grégoire Dooms
dooms at info.LESS.ucl.SPAM.ac.be
Sat Jun 12 08:38:20 EDT 2004
Larry Bates wrote:
> Something like this?
>
> def wordcount(input, sep=" "):
> global words
> if isinstance(input, str):
> words+=len([x.strip() for x in input.split(sep)])
What's the purpose of stripping the items in the list if you just count
their number ? Isn't this equivalent to
words += len(input.split(sep))
> return words
> else:
> for item in input:
> wordcount(item)
>
> return words
Removing the global statement and sep param, you get:
def wordcount(input):
if isinstance(input, str):
return len(input.split())
else:
return sum([wordcount(item) for item in input])
--
Grégoire Dooms
More information about the Python-list
mailing list