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.

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 2014-12-04 21:18 +1100
Subject Re: How is max supposed to work, especially key.
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16552.1417688329.18130.python-list@python.org> (permalink)

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