Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81646
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.05; 'true,': 0.05; 'element': 0.07; 'float': 0.07; 'returned.': 0.07; 'string': 0.09; 'false,': 0.09; 'key.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; 'boolean': 0.16; 'evaluates': 0.16; 'given,': 0.16; 'iterator': 0.16; 'optional': 0.16; 'range,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'elements': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'value.': 0.19; '>>>': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'van': 0.27; 'function': 0.29; "doesn't": 0.30; 'code': 0.31; 'calculated': 0.31; 'clever': 0.31; 'pan': 0.31; 'anyone': 0.31; 'says': 0.33; 'moment': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'false': 0.36; 'sequence': 0.36; "didn't": 0.36; 'too': 0.37; 'list': 0.37; 'handle': 0.38; 'to:addr:python-list': 0.38; 'expect': 0.39; 'explain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'greatest': 0.60; 'first': 0.61; 'maximum': 0.63; 'more': 0.64; 'account': 0.65; 'saw': 0.77; '102': 0.84; 'longest': 0.84; 'max.': 0.84; 'pivoting': 0.84; 'albert': 0.91; 'cast': 0.91; 'good,': 0.91; 'that),': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: How is max supposed to work, especially key. |
| Date | Thu, 27 Nov 2014 19:01:21 +0100 |
| Organization | None |
| References | <547760b3$0$6876$e4fe514c@dreader36.news.xs4all.nl> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd8493.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| 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.16378.1417111312.18130.python-list@python.org> (permalink) |
| Lines | 63 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417111312 news.xs4all.nl 2952 [2001:888:2000:d::a6]:59510 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81646 |
Show key headers only | 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
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