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.

From random832@fastmail.us
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 2014-12-04 09:09 -0500
Newsgroups comp.lang.python
Message-ID <mailman.16560.1417702186.18130.python-list@python.org> (permalink)

Show all headers | 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