Re: min, max with position
On Sat, Jun 4, 2022 at 9:07 PM Greg Ewing wrote: > On 5/06/22 10:07 am, dn wrote: > > On 05/06/2022 09.50, Chris Angelico wrote: > > min(enumerate(l), key=lambda x: x[1]) > >> (0, 1.618033) > > > > But, but, but which of the above characters is an 'el' and which a > 'one'??? > > (please have pity on us old f...s and the visually-challenged!) > > > > ell = l > one = 1 > min(enumerate(ell), key=lambda x: x[one]) > > Hope that clears it up!11!one!ell > I'm kind of partial to: min((value, index) for (index, value) in enumerate(list_)) It'll return a single 2-tuple where the value of interest is at position 0. In the event of a tie, it should give you the first such value encountered. -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
On 5/06/22 10:07 am, dn wrote: On 05/06/2022 09.50, Chris Angelico wrote: min(enumerate(l), key=lambda x: x[1]) (0, 1.618033) But, but, but which of the above characters is an 'el' and which a 'one'??? (please have pity on us old f...s and the visually-challenged!) ell = l one = 1 min(enumerate(ell), key=lambda x: x[one]) Hope that clears it up!11!one!ell -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
On Sun, 5 Jun 2022 at 08:09, dn wrote: > > On 05/06/2022 09.50, Chris Angelico wrote: > > No, but it shouldn't be too hard to make it if you want it. The > > obvious option of calling max/min on the enumerated list won't work on > > its own, since the index comes before the value, but with a key > > function it would work fine: > > > min(enumerate(l), key=lambda x: x[1]) > > (0, 1.618033) > max(enumerate(l), key=lambda x: x[1]) > > (1, 3.141593) > > An elegant solution! > > But, but, but which of the above characters is an 'el' and which a 'one'??? > (please have pity on us old f...s and the visually-challenged!) > Fair point, but I stuck to the OP's example list and kept it called 'l' for list :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
On 05/06/2022 09.50, Chris Angelico wrote: > No, but it shouldn't be too hard to make it if you want it. The > obvious option of calling max/min on the enumerated list won't work on > its own, since the index comes before the value, but with a key > function it would work fine: > min(enumerate(l), key=lambda x: x[1]) > (0, 1.618033) max(enumerate(l), key=lambda x: x[1]) > (1, 3.141593) An elegant solution! But, but, but which of the above characters is an 'el' and which a 'one'??? (please have pity on us old f...s and the visually-challenged!) -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
On Sun, 5 Jun 2022 at 08:00, dn wrote: > > On 05/06/2022 06.56, Dennis Lee Bieber wrote: > > On Sat, 4 Jun 2022 13:36:26 -0500, "Michael F. Stemper" > > declaimed the following: > > > >> > >> Are there similar functions that return not only the minimum > >> or maximum value, but also its position? > >> > > If it isn't in the library reference manual, NO... > > > > But it also isn't that difficult to write... > > > def whatAt(function, data): > > ... what = function(data) > > ... at = data.index(what) > > ... return (at, what) > > ... > l = [ 1.618033, 3.1415923536, 2.718282] > whatAt(min, l) > > (0, 1.618033) > whatAt(max, l) > > (1, 3.1415923536) > > > > > (properly, I should either reverse the order of the return value, or change > > the name to atWhat() ) > > ...and remembering the special case: > if the what value appears more than once in the list, the where?at will > report the first/'left-most' index only. > Which is consistent with the vanilla min() function. >>> min([1, 1.0]) 1 ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
On 05/06/2022 06.56, Dennis Lee Bieber wrote: > On Sat, 4 Jun 2022 13:36:26 -0500, "Michael F. Stemper" > declaimed the following: > >> >> Are there similar functions that return not only the minimum >> or maximum value, but also its position? >> > If it isn't in the library reference manual, NO... > > But it also isn't that difficult to write... > def whatAt(function, data): > ... what = function(data) > ... at = data.index(what) > ... return (at, what) > ... l = [ 1.618033, 3.1415923536, 2.718282] whatAt(min, l) > (0, 1.618033) whatAt(max, l) > (1, 3.1415923536) > > (properly, I should either reverse the order of the return value, or change > the name to atWhat() ) ...and remembering the special case: if the what value appears more than once in the list, the where?at will report the first/'left-most' index only. -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
On Sun, 5 Jun 2022 at 07:46, Michael F. Stemper wrote: > > Python contains built-in functions that return the minimum or > maximum items in a list. > > >>> l = [1.618033,3.141593,2.718282] > >>> min(l) > 1.618033 > >>> max(l) > 3.141593 > >>> > > Are there similar functions that return not only the minimum > or maximum value, but also its position? > > >>> specialmin(l) > (0,1.618033) > >>> specialmax(l) > 3.141593 > >>> > No, but it shouldn't be too hard to make it if you want it. The obvious option of calling max/min on the enumerated list won't work on its own, since the index comes before the value, but with a key function it would work fine: >>> min(enumerate(l), key=lambda x: x[1]) (0, 1.618033) >>> max(enumerate(l), key=lambda x: x[1]) (1, 3.141593) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
on a statement followed by an expression
(*) Question How can I, in a single line, write a statement followed by an expression? For example, if /d/ is a dicionary, how can I write d["key"] = value # and somehow making this line end up with d (*) Where does the question come from? >From the following experiment-exercise. (*) Introduction Here's a gentle way to consome records /rs/, each of which represents a robbery, say, and produce a dictionary containing a count of each zip code. --8<---cut here---start->8--- def zip(r): return r[0] def roberry_per_zip(rs): d = {} for r in rs: d[zip(r)] = dict.get(d, zip(r), 0) + 1 return d --8<---cut here---end--->8--- Now I'd like to compare the code with a version that uses reduce. Let me please write my own reduce function for completeness and clarity --- I suppose. The code is still pretty clear. --8<---cut here---start->8--- def my_reduce(it, f, init): r = init for e in it: r = f(r, e) return r def count_in(d, r): d[zip(r)] = dict.get(d, zip(r), 0) + 1 return d def roberry_via_reduce(rs): return my_reduce(rs, count_in, {}) --8<---cut here---end--->8--- It's not clear, though, how to write such a procedure using a lambda expression in place of count_in. That's because we must return a dicionary and statements are not expressions. How can I execute a statement followed by a value in a single line? def roberry_via_reduce(rs): return my_reduce(rs, lambda d, r: ``increment and return d'', {}) I don't know how to write ``increment and return d'' I'm just curious. Thank you. -- https://mail.python.org/mailman/listinfo/python-list
Re: on a statement followed by an expression
Meredith Montgomery writes: How can I execute a statement followed by a value in a single line? def roberry_via_reduce(rs): return my_reduce(rs, lambda d, r: ``increment and return d'', {}) The grammar or Python is deliberately designed so that the body of a lambda expression cannot contain any statements. So your original code, where you defined a separate `count_in' procedure, is probably what you want. Although using `reduce' is kind of silly in this case because you aren't computing a new dictionary every time, but instead you are mutating the _same_ dictionary every time. Your original code that used a `for' loop is actually much clearer. -- Alan Bawden -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
On 04/06/2022 13.56, Dennis Lee Bieber wrote: On Sat, 4 Jun 2022 13:36:26 -0500, "Michael F. Stemper" declaimed the following: Are there similar functions that return not only the minimum or maximum value, but also its position? If it isn't in the library reference manual, NO... But it also isn't that difficult to write... I already wrote it. I was wondering if there was something standard that I should have used instead. I have multiple functions that I wrote, only to later learn that there were standard functions that did the same thing. -- Michael F. Stemper This email is to be read by its intended recipient only. Any other party reading is required by the EULA to send me $500.00. -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
"Michael F. Stemper" writes: Are there similar functions that return not only the minimum or maximum value, but also its position? >>> specialmin(l) (0,1.618033) >>> specialmax(l) 3.141593 >>> I believe that what you are looking for is usually called "argmax" and "argmin" (see ). These don't exist in the standard Python library as far as I can tell, but numpy does have "argmax" and "argmin" routines. -- Alan Bawden -- https://mail.python.org/mailman/listinfo/python-list
Re: min, max with position
On Sat, 4 Jun 2022 13:36:26 -0500, "Michael F. Stemper" declaimed the following: > >Are there similar functions that return not only the minimum >or maximum value, but also its position? > If it isn't in the library reference manual, NO... But it also isn't that difficult to write... >>> def whatAt(function, data): ... what = function(data) ... at = data.index(what) ... return (at, what) ... >>> l = [ 1.618033, 3.1415923536, 2.718282] >>> whatAt(min, l) (0, 1.618033) >>> whatAt(max, l) (1, 3.1415923536) >>> (properly, I should either reverse the order of the return value, or change the name to atWhat() ) -- Wulfraed Dennis Lee Bieber AF6VN wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list
min, max with position
Python contains built-in functions that return the minimum or maximum items in a list. >>> l = [1.618033,3.141593,2.718282] >>> min(l) 1.618033 >>> max(l) 3.141593 >>> Are there similar functions that return not only the minimum or maximum value, but also its position? >>> specialmin(l) (0,1.618033) >>> specialmax(l) 3.141593 >>> -- Michael F. Stemper I feel more like I do now than I did when I came in. -- https://mail.python.org/mailman/listinfo/python-list