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


Groups > comp.lang.python > #81908

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

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 <random832@fastmail.us>
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> <mailman.16378.1417111312.18130.python-list@python.org> <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 <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.16560.1417702186.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | Next — Previous 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