Re: min, max with position

2022-06-05 Thread Michael F. Stemper

On 04/06/2022 14.08, Stefan Ram wrote:

"Michael F. Stemper"  writes:

Are there similar functions that return not only the minimum
or maximum value, but also its position?


   The maximum value does not need to have a unique position.


Which is something that I'll just have to live with, whether I
use home-brew or something from a standard module. For the particular
application, my lists have 24 elements and are converted from data
that is received in a %5.2f format, so collisions aren't too likely
anyway.


   The World-Wide Web holds several suggestions like l.index(
   max( l )) or max( enumerate( l ), key=( lambda x: x[ 1 ])).


Oh, I like that second one. I tried playing around with a lambda
function, but combining with enumerate eluded me. It's much better
than my for-loop with extraneous variables.

--
Michael F. Stemper
Psalm 94:3-6
--
https://mail.python.org/mailman/listinfo/python-list


Re: min, max with position

2022-06-05 Thread Antoon Pardon




Op 5/06/2022 om 01:52 schreef Greg Ewing:

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])


I'd like to point to the operator module which allows you to write the 
above as:


min(enumerate(ell), key=operator.itemgetter(one))

--
Antoon Pardon.
--
https://mail.python.org/mailman/listinfo/python-list