Howegrown wordcount
Thomas Philips
tkpmep at hotmail.com
Fri Jun 11 14:05:32 EDT 2004
I've coded a little word counting routine that handles a reasonably
wide range of inputs. How could it be made to cover more, though
admittedly more remote, possibilites such as nested lists of lists,
items for which the string representation is a string containing lists
etc. etc. without significantly increasing the complexity of the
program?
Thomas Philips
def wordcount(input):
from string import whitespace
#Treat iterable inputs differently
if "__iter__" in dir(input):
wordList =(" ".join([str(item) for item in input])).split()
else:
wordList = [str(input)]
#Remove any words that are just whitespace
for i,word in enumerate(wordList):
while word and word[-1] in whitespace:
word = word[:-1]
wordList[i] = word
wc = len(filter(None,wordList)) #Filter out any empty strings
return wc
More information about the Python-list
mailing list