Learning Python via a little word frequency program

Paul Rubin http
Fri Jan 11 12:50:53 CET 2008


rent <rentlong at gmail.com> writes:
> keys = freq.keys()
> keys.sort(key = freq.get, reverse = True)
> for k in keys:
>      print "%-10s: %d" % (k, freq[k])

I prefer (untested):

  def snd((x,y)): return y   # I wish this was built-in
  sorted_freq = sorted(freq.iteritems(), key=snd, reverse=True)
  for k,f in sorted_freq:
     print "%-10s: %d" % (k, f)



More information about the Python-list mailing list