Python: get the maximum pair in a list of pairs with min y

How can I get the max pair in the list of pairs with min y?

I got this list:

L =[[1,3],[2,5],[-4,0],[2,1],[0,9]]

With max (L), I get [2.5], but I want [2.1].

+3
source share
3 answers
max(L, key=lambda item: (item[0], -item[1]))

Conclusion:

[2, 1]
+17
source

Your query is rather cryptic, but I think this is what you want:

x, y = zip(*L)
maxPairs = [L[i] for i,a in enumerate(x) if a == max(x)]
returnPair = sorted(maxPairs)[0]
+1
source
import operator

get_y= operator.itemgetter(1)
min(L, key=get_y)[0]

Finds a coordinate with a minimum of y, extracts x.

If you do not like it operator.itemgetter, do:

min(L, key=lambda c: c[1])[0]
0
source

Source: https://habr.com/ru/post/1794725/


All Articles