On Tue, Jul 19, 2011 at 10:10 PM, CM <cmpyt...@gmail.com> wrote: > On Jul 19, 11:17 pm, CM <cmpyt...@gmail.com> wrote: >> I have three items in a dict, like this: >> >> the_dict = {'a':1, 'b':2, 'c':3} >> >> but the vals could be anything. I want to configure something else >> based on the "winner" of such a dict, with these rules: <snip> > I realize, now though, (and Chris asked about this) that I was > imprecise in my > rules. They really should be stated as: > > 1. In this dict, if there is a UNIQUE max value, then its *key* is the > winner. > 2. If there are any TIES for max value, then the *key* 'b' is the > winner by default. > > The point is, I am trying to determine the name of the winning > category, either > 'a', 'b', or 'c', not the value of its winning score. > > So in your solutions there is sorting by values, which makes sense. > But how > can I go back to keys from there? Sorry for the mistake (but even so, > I learned > something already).
# still presumes at least 2 items from heapq import nlargest winner, runner_up = nlargest(2, the_dict, lambda k: the_dict[k]) if the_dict[winner] == the_dict[runner_up]: winner = 'b' Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list