large dictionary creation takes a LOT of time.

Maksim Kasimov kasimov at i.com.ua
Fri Apr 29 09:04:18 EDT 2005


Kent Johnson wrote:
> 
> Here is a little cleaner version. It takes about a second to run on my 
> PC. What hardware are you running on?
> 
> path = 'DonQuixote.txt'
> 
> frequency = {}
> 
> for line in open(path):
>     for word in line.split():
>         if frequency.has_key(word):
>             frequency[word] += 1
>         else:
>             frequency[word] = 1
> 
> print len(frequency), 'words'
> 
> 
> Kent

 > for line in open(path):
the line of your example raise another question: opened file will be read at once time, as method readlines() do, or it will be read line by line as method readline() do.
as far i know, it is depends on implementation of method "__iter__" of the object that "open()" returns, so another question: where i can find such an information (about how does such a functions works)?


-- 
Best regards,
Maksim Kasimov
mailto: kasimov at i.com.ua



More information about the Python-list mailing list