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: [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list

Reply via email to