count items in generator

Alex Martelli aleax at mac.com
Sun May 14 01:27:39 EDT 2006


BartlebyScrivener <rpdooling at gmail.com> wrote:

> Still new. I am trying to make a simple word count script.
> 
> I found this in the great Python Cookbook, which allows me to process
> every word in a file. But how do I use it to count the items generated?
> 
> def words_of_file(thefilepath, line_to_words=str.split):
>     the_file = open(thefilepath)
>     for line in the_file:
>         for word in line_to_words(line):
>                 yield word
>     the_file.close()
> for word in words_of_file(thefilepath):
>     dosomethingwith(word)
> 
> The best I could come up with:
> 
> def words_of_file(thefilepath, line_to_words=str.split):
>     the_file = open(thefilepath)
>     for line in the_file:
>         for word in line_to_words(line):
>                 yield word
>     the_file.close()
>     len(list(words_of_file(thefilepath)))
> 
> But that seems clunky.

My preference would be (with the original definition for
words_of_the_file) to code

   numwords = sum(1 for w in words_of_the_file(thefilepath))


Alex



More information about the Python-list mailing list