On Jun 4, 11:28 am, Paul Rubin <no.em...@nospam.invalid> wrote: > kj <no.em...@please.post> writes: > > 1. is there a standard name for it? > > I don't know of one, or a stdlib for it, but it's pretty trivial. > > > def tally(c): > > t = dict() > > for x in c: > > t[x] = t.get(x, 0) + 1 > > return sorted(t.items(), key=lambda x: (-x[1], x[0])) > > I like to use defaultdict and tuple unpacking for code like that: > > from collections import defaultdict > def tally(c): > t = defaultdict(int) > for x in c: > t[x] += 1 > return sorted(t.iteritems(), key=lambda (k,v): (-v, k))
I would also very much like to see this become part of the standard library. Sure the code is easy to write but I use this incredibly often and I've always wished I would have a one-line function call that has the same output as the mysql query: "SELECT id, count(*) FROM table GROUP BY somefield" or maybe there is already a short solution to this that I'm not aware of... -- http://mail.python.org/mailman/listinfo/python-list