Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!novia!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.01; 'python.': 0.04; 'value,': 0.04; 'default.': 0.09; 'configure': 0.11; 'wrote:': 0.15; 'it".': 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; 'winner.': 0.16; '\xc2\xa0i': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; 'this:': 0.16; 'cheers,': 0.19; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'elegant': 0.23; 'tue,': 0.23; 'function': 0.26; 'knowing': 0.28; 'message-id:@mail.gmail.com': 0.28; 'import': 0.29; 'matches': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.30; 'least': 0.31; 'chris': 0.32; 'there': 0.34; 'but,': 0.34; 'test': 0.34; 'probably': 0.35; '(to': 0.37; 'but': 0.37; 'could': 0.37; 'received:google.com': 0.38; 'received:209.85.161': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'something': 0.38; 'else': 0.38; 'ways': 0.39; 'subject:with': 0.39; 'received:209': 0.40; 'unique': 0.64; 'anything.': 0.71; 'winner': 0.76; 'subject:one': 0.77; 'dict,': 0.84; 'distinguish': 0.84; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84; '"one': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=P2JiuUPyIkAT+tH9b1WNHsKwCzxXFkS58aUF2dMTdrg=; b=Asxg/h9yp/JZdaqtf9elQ/iTHBVmUJPvQcpXPMUQ8XCrlN22gfxX2ZvTY5a/rfnBmS WH/y9iP1OSzHWPCt/7TEvbrDIUa5b3tje0f7algpjNd4je8+lrkxASecT6epYkVb8vAe odKB4RTfN8YII12pSikhtZ2Kjg6NHxdKU7WHU= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Tue, 19 Jul 2011 21:33:18 -0700 X-Google-Sender-Auth: by-qsJwO1u_obO2lL9Xo_Zj2oL8 Subject: Re: Pythonic way with more than one max possible From: Chris Rebert To: CM Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311136401 news.xs4all.nl 23846 [2001:888:2000:d::a6]:41014 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9935 On Tue, Jul 19, 2011 at 8:17 PM, CM wrote: > I have three items in a dict, like this: > > the_dict =3D {'a':1, 'b':2, 'c':3} > > but the vals could be anything. =C2=A0I 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. Er, for (2), you mean b's /value/ is the winner, right? > The problem for me, as I see it, is I don't know any elegant ways to > do this in Python. =C2=A0The max(dict) function doesn't distinguish betwe= en > unique and non-unique maxes. =C2=A0I could go through and test the variou= s > 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". =C2=A0Any suggestions? # presumes at least 2 items from heapq import nlargest winner, runner_up =3D nlargest(2, the_dict.itervalues()) if winner =3D=3D runner_up: winner =3D the_dict['b'] Cheers, Chris -- http://rebertia.com