Ant a écrit : >> I'm interested to learn how more experienced Python folks would solve >> this little problem. > > I think I'd do the following: > > from collections import defaultdict > > names = "freddy fred bill jock kevin andrew kevin kevin jock" > freq = defaultdict(lambda: 0) > > for name in names.split(): > freq[name] += 1 > > pairs = [(v, k) for k, v in freq.iteritems()] > > for v, k in reversed(sorted(pairs)): > print "%-10s: %d" % (k, v) > > > defaultdict makes the frequency accumulation neater. > > reversed(sorted(pairs)) avoids the little -v hack and makes it more > obvious what you are doing.
But fails to implement the specs (emphasis is mine): """ produce a frequency table of names, sorted descending by frequency. *then ascending by name*. For the above data, the output should be: kevin : 3 jock : 2 andrew : 1 bill : 1 fred : 1 freddy : 1 """ With your solution, you get: kevin : 3 jock : 2 freddy : 1 fred : 1 bill : 1 andrew : 1 -- http://mail.python.org/mailman/listinfo/python-list