Return-Path: 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> <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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, Dec 4, 2014 at 9:09 PM, Albert van der Horst 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