Howegrown wordcount

Larry Bates lbates at swamisoft.com
Fri Jun 11 14:50:34 EDT 2004


Something like this?

def wordcount(input, sep=" "):
    global words
    if isinstance(input, str):
        words+=len([x.strip() for x in input.split(sep)])
        return words
    else:
        for item in input:
            wordcount(item)

    return words

#
# Test with a string
#
words=0
print wordcount("This is a test") # String test
words=0
print wordcount(["This is a test", "This is a test"]) # List test
words=0
print wordcount([["This is a test","This is a test"],
                 ["This is a test","This is a test"]]) # List of lists
words=0
data=[["this is a test"],["this", "is", "a", "test"],"This is a test"]
print wordcount(data)

HTH,
Larry Bates


"Thomas Philips" <tkpmep at hotmail.com> wrote in message
news:b4a8ffb6.0406111005.d386075 at posting.google.com...
> 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