Sorting distionary by value

Jeff Shannon jeff at ccvcorp.com
Fri Mar 22 16:11:06 EST 2002


Artur Skura wrote:

> for i in a:
>     if i not in known:
>         for l in a:
>             if l == i:
>                 times = times + 1
>         known.append(i)
>         output[i] = times
>     times = 0
>
> it seems it's slow not because of sorting...

Yes, you're scanning through the file once for *each* word you find, just to
count occurences.  Try this instead:

words = string.split(open(sys.argv[1],'r').read())

wordcount = {}

for word in words:
    wordcount[word] = wordcount.get(word, 0) + 1

items = [ (value, key) for key, value in wordcount.items()  ]
items.sort()
items.reverse()
#  items = [ (k, v) for (v, k) in items ]
#  Why bother with the above??

for count, word in items:
    print word, count

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list