Sorting distionary by value

Paul Rubin phr-n2002a at nightsong.com
Fri Mar 22 17:11:26 EST 2002


Artur Skura <arturs at iidea.pl> writes:
> No, and it seems the problem is not with sorting.
> I wanted to write a compact word counting script (well, in shell
> it can be done in a 5 lines or so), just  for fun.
>...
> for i in a:
>     if i not in known:

This is horrendously slow because for every input word, you're comparing
it against all the previously seen words.

> it seems it's slow not because of sorting...

Correct.  I didn't examine your code carefully enough to be sure, but
what I think you want is something like this:

counts = {}
a = string.split(open(sys.argv[1],'r').read())
for w in a:
  if counts.has_key(w):
     counts[w] += 1
  else:
     counts[w] = 1

words = counts.keys()
words.sort()
words.reverse()

for w in words:
   print words[w], w



More information about the Python-list mailing list