Pythonic way with more than one max possible

CM cmpython at gmail.com
Wed Jul 20 01:10:32 EDT 2011


On Jul 19, 11:17 pm, CM <cmpyt... at 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:
>
> 1. In this dict, if there is a UNIQUE max value, that's the winner.
> 2. If there are any TIES for max value, b is the winner by default.
>

Thank you to Steven and Chris for the answers.  Very elegant.

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).

Thanks again,
Che




More information about the Python-list mailing list