[Tutor] Simple counter to determine frequencies of words in adocument

Alan Gauld alan.gauld at btinternet.com
Sun Nov 21 01:49:36 CET 2010

"col speed" <ajarncolin at gmail.com> wrote

> Just my twopenneth, I'm a noob and I'm not going to try such a big 
> file on
> my old machine, but:
> 1. Maybe create a *set* from the wordlist, loop through that, so you 
> call
> "count" on wordlist only once.  OR

This would be an improvement but still involves traversing the entire
list N times where N is the number of unique words.

> 2. Use collections.defaultdict(int) and loop through wordlist and do
> dic[word] += 1

This is what his second version is supposed to do and is the
best solution since it only involves a single trasverse of the file.

Alan G. 

More information about the Tutor mailing list