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


Groups > comp.lang.python > #81646

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

From Peter Otten <__peter__@web.de>
Subject Re: How is max supposed to work, especially key.
Date 2014-11-27 19:01 +0100
Organization None
References <547760b3$0$6876$e4fe514c@dreader36.news.xs4all.nl>
Newsgroups comp.lang.python
Message-ID <mailman.16378.1417111312.18130.python-list@python.org> (permalink)

Show all headers | View raw


Albert van der Horst wrote:

> In the Rosetta code I come across this part of
> LU-decomposition.
> 
> def pivotize(m):
>     """Creates the pivoting matrix for m."""
>     n = len(m)
>     ID = [[float(i == j) for i in xrange(n)] for j in xrange(n)]
>     for j in xrange(n):
>         row = max(xrange(j, n), key=lambda i: abs(m[i][j]))
>         if j != row:
>             ID[j], ID[row] = ID[row], ID[j]
>     return ID
> 
> That it's using a cast from boolean to float and using
> at the other moment a float as a boolean, suggest that this
> code is a bit too clever for its own good, but anyway.
> 
> My problem is with the max. I never saw a max with a key.
> 
> In my python help(max) doesn't explain the key. It says that
> max can handle an iterator (I didn't know that), and you can
> pass and optional "key=func", but that's all.
> 
> I expect it to be something like
>   elements in the iterator are taken into account only if the
>   key applied to the iterator evaluates to a True value.
> 
> However that doesn't pan out:
> "
> max(xrange(100,200), key=lambda i: i%17==0 )
> 102
> "
> 
> I expect the maximum number that is divisible by 17 in the
> range, not the minimum.
> 
> Can anyone shed light on this?

Given a function f() max(items, key=f) returns the element of the `items` 
sequence with the greatest f(element), e. g. for

max(["a", "bcd", "ef"], key=len)

the values 1, 3, 2 are calculated and the longest string in the list is 
returned:

>>> max(["a", "bcd", "ef"], key=len)
'bcd'

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.

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