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


Groups > comp.lang.python > #81890

Re: How is max supposed to work, especially key.

Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'true,': 0.05; 'returned.': 0.07; 'false,': 0.09; 'part,': 0.09; 'subject:How': 0.10; 'cc:addr :python-list': 0.11; '(either': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'skip:> 20': 0.16; 'third,': 0.16; 'tuple,': 0.16; 'exception': 0.16; 'wrote:': 0.18; "python's": 0.19; 'thu,': 0.19; 'cc:addr:python.org': 0.22; "shouldn't": 0.24; 'cc:2**0': 0.24; 'values': 0.27; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'raise': 0.29; "doesn't": 0.30; 'dec': 0.30; 'have,': 0.30; 'message- id:@mail.gmail.com': 0.30; 'largest': 0.30; 'that.': 0.31; 'calculated': 0.31; "can't": 0.35; 'something': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'picking': 0.36; 'returning': 0.36; 'useful': 0.36; 'clear': 0.37; 'being': 0.38; 'whatever': 0.38; 'pm,': 0.38; 'does': 0.39; 'called': 0.40; 'first': 0.61; 'maximum': 0.63; 'myself': 0.63; 'pick': 0.64; 'more': 0.64; 'max.': 0.84; 'albert': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=tZ7lGKEx/cS2vQNrQeWM3Jda/gNraMBrv58zoeFihLM=; b=wv5/Edifc58hLrzLW20wixOc/snkYb3+Wjx41EPKG8bTv0rIjnqfgYb4H4aX2PI7Hq XZJzqNaxfElZ4eajsOC6U3fHrQH3Q9Yziv0hhSZYfvAnqcI/4iIfwMKDZeE4x7shwJB1 iiECCBWDbk4MaYDKfv27NhZbQfr0X0D7XC/oAYiDTO3gPWUSyz4SnvF6JjAL/hjlj5YV kQ7INNYGOv6X5+kiZj8esrX0FQACQFPLisDN5279C2nAJ3AwfXEXjKaOK5LJz/XxqA1c wp1Wtiuk718bVWG8xydfHcorXza9mCu2ol9kcJykUnVaFNkIAVteK8nNKSkSPBIB7J2Q NbeQ==
MIME-Version 1.0
X-Received by 10.107.17.79 with SMTP id z76mr8692828ioi.27.1417688326389; Thu, 04 Dec 2014 02:18:46 -0800 (PST)
In-Reply-To <548032dd$0$15926$e4fe514c@dreader35.news.xs4all.nl>
References <547760b3$0$6876$e4fe514c@dreader36.news.xs4all.nl> <mailman.16378.1417111312.18130.python-list@python.org> <548032dd$0$15926$e4fe514c@dreader35.news.xs4all.nl>
Date Thu, 4 Dec 2014 21:18:46 +1100
Subject Re: How is max supposed to work, especially key.
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16552.1417688329.18130.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417688329 news.xs4all.nl 2904 [2001:888:2000:d::a6]:59643
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:81890

Show key headers only | View raw


On Thu, Dec 4, 2014 at 9:09 PM, Albert van der Horst
<albert@spenarnc.xs4all.nl> wrote:
>>If there is more than one item with the maximum calculated the first is
>>given, so for your attempt
>>
>>max(xrange(100,200), key=lambda i: i%17==0 )
>
>>
>>the values False, False, True, False, ... are calculated and because
>>
>>>>> True > False
>>True
>>
>>the first one with a True result is returned.
>>
>
> So in that case max doesn't return the maximum (True), but instead
> something else.
>
> Useful as that function may be, it shouldn't have been called max.
>
> I don't blame myself for being misled.

If lots of them are equally the largest, by whatever definition of
largest you have, it has to do one of three things:

1) Raise an exception
2) Return multiple items (either as a tuple, or a generator, or something)
3) Pick one of them and return it.

Python's max() does the third, and for the picking part, uses the
first one it comes across - a decent way to do it.

If there's no clear maximum, it can't do any better than that. It's
still returning something for which there is no greater.

ChrisA

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


Thread

How is max supposed to work, especially key. albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-11-27 17:34 +0000
  Re: How is max supposed to work, especially key. Peter Otten <__peter__@web.de> - 2014-11-27 19:01 +0100
    Re: How is max supposed to work, especially key. albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-12-04 10:09 +0000
      Re: How is max supposed to work, especially key. Chris Angelico <rosuav@gmail.com> - 2014-12-04 21:18 +1100
        Re: How is max supposed to work, especially key. albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-12-04 10:35 +0000
          Re: How is max supposed to work, especially key. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-12-04 12:51 +0200
          Re: How is max supposed to work, especially key. Peter Otten <__peter__@web.de> - 2014-12-04 12:31 +0100
          Re: How is max supposed to work, especially key. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-04 23:03 +1100
          Re: How is max supposed to work, especially key. Terry Reedy <tjreedy@udel.edu> - 2014-12-04 10:47 -0500
      Re: How is max supposed to work, especially key. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-12-04 12:45 +0200
        Re: How is max supposed to work, especially key. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-04 22:55 +1100
          Re: How is max supposed to work, especially key. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-12-04 14:58 +0200
      Re: How is max supposed to work, especially key. Peter Otten <__peter__@web.de> - 2014-12-04 12:24 +0100
      Re: How is max supposed to work, especially key. random832@fastmail.us - 2014-12-04 09:09 -0500

csiph-web