Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9957

Re: Pythonic way with more than one max possible

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <t@jollybox.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'else:': 0.03; 'python.': 0.04; 'value,': 0.04; 'default.': 0.09; 'configure': 0.11; 'wed,': 0.12; 'wrote:': 0.15; 'it".': 0.16; 'received:192.168.1.40': 0.16; 'subject:Pythonic': 0.16; 'subject:max': 0.16; 'subject:possible': 0.16; 'suggestions?': 0.16; 'ties': 0.16; 'vals': 0.16; 'values),': 0.16; 'values[0]': 0.16; 'winner.': 0.16; 'this:': 0.16; 'def': 0.16; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'assumes': 0.23; 'elegant': 0.23; 'items.': 0.23; 'function': 0.26; 'knowing': 0.28; 'matches': 0.29; 'problem': 0.29; 'least': 0.31; 'values': 0.31; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'there': 0.34; 'but,': 0.34; 'test': 0.34; 'probably': 0.35; '(to': 0.37; 'but': 0.37; 'could': 0.37; 'received:192': 0.38; 'subject:: ': 0.38; 'steven': 0.38; 'something': 0.38; 'else': 0.38; 'two': 0.38; 'received:192.168.1': 0.39; 'ways': 0.39; 'subject:with': 0.39; 'skip:s 20': 0.39; 'to:addr:python.org': 0.39; 'unique': 0.64; 'received:62': 0.67; 'anything.': 0.71; 'winner': 0.76; 'subject:one': 0.77; 'dict,': 0.84; 'distinguish': 0.84; 'from:addr:t': 0.84; '"one': 0.91
Date Wed, 20 Jul 2011 12:51:39 +0200
From Thomas Jollans <t@jollybox.de>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110626 Iceowl/1.0b2 Icedove/3.1.11
MIME-Version 1.0
To python-list@python.org
Subject Re: Pythonic way with more than one max possible
References <acfc6e53-fd3c-4404-9314-aa01dc35c0d7@a31g2000vbt.googlegroups.com> <4e265748$0$29970$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <4e265748$0$29970$c3e8da3$5496439d@news.astraweb.com>
X-Enigmail-Version 1.1.2
OpenPGP id=5C8691ED
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1287.1311159102.1164.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311159102 news.xs4all.nl 23975 [2001:888:2000:d::a6]:50168
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:9957

Show key headers only | View raw


On 20/07/11 06:19, Steven D'Aprano wrote:
> On Wed, 20 Jul 2011 01:17 pm CM 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.
>>
>> The problem for me, as I see it, is I don't know any elegant ways to
>> do this in Python.  The max(dict) function doesn't distinguish between
>> unique and non-unique maxes.  I could go through and test the various
>> possibilities (to see if the max value had any matches in the other
>> values), but, knowing Python, there is probably something close to
>> "one way to do it".  Any suggestions?
> 
> # Untested.
> def get_winner(adict):
>     values = sorted(adict.values(), reverse=True)
>     if values[0] == values[1]:
>         return adict['b']
>     else:
>         return values[0]

# Untested, with keys:
def get_winner(adict):
    values = sorted(adict.items(), reverse=True,
                    key=(lambda k_v: k_v[1]))
    if values[0][1] == values[1][1]:
        return 'b'
    else:
        return values[0][0]

> 
> Assumes that adict has at least two items. May be slow if it has millions of
> items.
> 
> 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Pythonic way with more than one max possible CM <cmpython@gmail.com> - 2011-07-19 20:17 -0700
  Re: Pythonic way with more than one max possible Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-20 14:19 +1000
    Re: Pythonic way with more than one max possible Thomas Jollans <t@jollybox.de> - 2011-07-20 12:51 +0200
  Re: Pythonic way with more than one max possible Chris Rebert <clp2@rebertia.com> - 2011-07-19 21:33 -0700
  Re: Pythonic way with more than one max possible CM <cmpython@gmail.com> - 2011-07-19 22:10 -0700
    Re: Pythonic way with more than one max possible woooee <woooee@gmail.com> - 2011-07-19 22:37 -0700
    Re: Pythonic way with more than one max possible Chris Rebert <clp2@rebertia.com> - 2011-07-19 23:56 -0700
  Re: Pythonic way with more than one max possible CM <cmpython@gmail.com> - 2011-07-20 13:25 -0700

csiph-web