count items in generator
George Sakkis
george.sakkis at gmail.com
Sun May 14 01:33:25 EDT 2006
BartlebyScrivener 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.
As clunky as it seems, I don't think you can beat it in terms of
brevity; if you care about memory efficiency though, here's what I use:
def length(iterable):
try: return len(iterable)
except:
i = 0
for x in iterable: i += 1
return i
You can even shadow the builtin len() if you prefer:
import __builtin__
def len(iterable):
try: return __builtin__.len(iterable)
except:
i = 0
for x in iterable: i += 1
return i
HTH,
George
More information about the Python-list
mailing list