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


Groups > comp.lang.python > #9942

Re: Pythonic way with more than one max possible

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'value,': 0.04; 'rules.': 0.07; '*key*': 0.09; 'default.': 0.09; 'there?': 0.09; 'configure': 0.11; 'wrote:': 0.15; 'lambda': 0.16; 'sorting': 0.16; 'subject:Pythonic': 0.16; 'subject:max': 0.16; 'subject:possible': 0.16; 'this)': 0.16; 'ties': 0.16; 'vals': 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; '(but': 0.22; 'cc:no real name:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'trying': 0.23; 'tue,': 0.23; 'asked': 0.24; '(and': 0.27; 'message-id:@mail.gmail.com': 0.28; 'import': 0.29; 'cc:addr:python.org': 0.30; 'values,': 0.30; 'least': 0.31; 'chris': 0.32; 'there': 0.34; 'keys': 0.35; 'but': 0.37; 'could': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'something': 0.38; 'else': 0.38; 'should': 0.39; 'either': 0.39; 'subject:with': 0.39; 'determine': 0.40; 'received:209': 0.40; 'your': 0.60; 'back': 0.63; 'unique': 0.64; 'stated': 0.65; 'as:': 0.71; 'anything.': 0.71; 'learned': 0.73; 'winner': 0.76; 'subject:one': 0.77; 'dict,': 0.84; 'sender:addr:chris': 0.84; 'received:209.85.218.46': 0.91; 'received:mail-yi0-f46.google.com': 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=Wdb9QgS/dnspnzDkFE1c+9L9hHv258jlrs/pFFCVWnE=; b=bi4L87urMBObD4rrP7qrewSgzRBIcp6JSHAY3PsltgRNKDjorJBJ6WLJcjAOfByllS oe3eIlSN3xcK5zKwe78uK9jOPK3OUoAs6TRy2cQcixKnEga9OsWlys9QrhHrn//zX3Xd Nx4MEpt8TN093RXuwZ4FCXmt3WBtecjjQVk8g=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <6e96527b-e41c-4f1a-b829-a2fa7648eaa3@t9g2000vbs.googlegroups.com>
References <acfc6e53-fd3c-4404-9314-aa01dc35c0d7@a31g2000vbt.googlegroups.com> <6e96527b-e41c-4f1a-b829-a2fa7648eaa3@t9g2000vbs.googlegroups.com>
Date Tue, 19 Jul 2011 23:56:51 -0700
X-Google-Sender-Auth XA7Nk5cHH_gCPQXYwQutVd9rHF4
Subject Re: Pythonic way with more than one max possible
From Chris Rebert <clp2@rebertia.com>
To CM <cmpython@gmail.com>
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 <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.1280.1311145014.1164.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311145014 news.xs4all.nl 23907 [2001:888:2000:d::a6]:55178
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:9942

Show key headers only | View raw


On Tue, Jul 19, 2011 at 10:10 PM, CM <cmpython@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

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