Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'binary': 0.07; 'string': 0.09; 'input,': 0.09; 'received:internal': 0.09; 'way:': 0.09; 'subject:How': 0.10; 'bool': 0.16; 'lambda': 0.16; 'list),': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'messy': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:66.111.4.27': 0.16; 'received:messagingengine.com': 0.16; 'received:out3-smtp.messagingengine.com': 0.16; 'uppercase': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'thu,': 0.19; 'proxy': 0.24; '(or': 0.24; 'compare': 0.26; 'van': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'compared': 0.30; 'dec': 0.30; 'returned': 0.30; 'comparison': 0.31; 'object.': 0.31; 'operators': 0.31; 'supposed': 0.32; 'actual': 0.34; '"the': 0.34; 'maybe': 0.34; 'received:66': 0.35; 'something': 0.35; 'but': 0.35; 'version': 0.36; 'two': 0.37; 'list': 0.37; 'list.': 0.37; 'received:10': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'according': 0.40; 'from:no real name:2**0': 0.61; 'simply': 0.61; 'first': 0.61; 'header:Message- Id:1': 0.63; 'maximum': 0.63; '2014,': 0.84; 'by.': 0.84; 'different.': 0.84; 'albert': 0.91 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.us; h= message-id:x-sasl-enc:from:to:mime-version :content-transfer-encoding:content-type:in-reply-to:references :subject:date; s=mesmtp; bh=KyvL83Jp8qYycWR5hjErs2zDJR4=; b=OTMn ZbIxudqpee9XViKepgWDGBlyFrIuqMuv23JqBe9JTPC0H3LVVmlxVbqjsRMGmQIV CGvgzvpjtH5lbSzBIV75SYCoBwZrTISZy0xWKNCW2486stTLFKPqccAvJ93Ms5xv Pu/4lmdN4Tow6TVhceJiuaWiG3WK/nGyXXO4/9E= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:x-sasl-enc:from:to :mime-version:content-transfer-encoding:content-type:in-reply-to :references:subject:date; s=smtpout; bh=KyvL83Jp8qYycWR5hjErs2zD JR4=; b=Z+YwnBOd1qPXniLxKbgScddeQWGjHlu9cDYIV1BDSg8+b1MFOUJW0trf bB8SvkTkrHnfbCtu8vmXiGofMNl9awH+rx4OQe70nbFfU/nrOoA/CTdCbWMgwu5s mnaroWS914hunotm1IoJehHQU4nt8c5XP3uqiFhZ9YkiLtsRthU= X-Sasl-Enc: TcuDoQwPyitlO2gWQrUvkoIQ/gHFOv25iJWlua4Ln4Ll 1417702183 From: random832@fastmail.us To: python-list@python.org MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain X-Mailer: MessagingEngine.com Webmail Interface - ajax-53201334 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> Subject: Re: How is max supposed to work, especially key. Date: Thu, 04 Dec 2014 09:09:43 -0500 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417702186 news.xs4all.nl 2930 [2001:888:2000:d::a6]:46048 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81908 On Thu, Dec 4, 2014, at 05:09, Albert van der Horst wrote: > So in that case max doesn't return the maximum (True), but instead > something else. If you want to find the "largest" item in a list of of strings, sorted case-insensitively, you might use str.lower or locale.strxfrm as the key function. If two strings are case-insensitively identical, using str.lower will return an arbitrary one (or maybe the first or last found in the original list), using locale.strxfrm will return the one that is in uppercase in the first position where they are different. But you don't want it to return the all-lowercase version (or the messy binary thing returned by strxfrm), you want it to return the string from the original list. Think of it this way: the key function returns a proxy object whose comparison operators compare according to the relationship you want to compare the originals by. Bool does not make a good proxy object. If you want to use the original as a tiebreaker, you might use lambda x: (str.lower(x), x). A key function is supposed to be a function that will always return the same result on the same input, so if you want to know "the maximum" [i.e. the actual key value that was compared by the max function] you can simply apply it again on the result.