Python:在min y的对列表中获取最大对

时间:2022-09-17 22:51:07

How i get max pair in a list of pairs with min y?

我如何在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].

使用max(L)我得到[2,5],但我想要[2,1]。

3 个解决方案

#1


17  

max(L, key=lambda item: (item[0], -item[1]))

Output:

输出:

[2, 1]

#2


1  

Your request is kind of 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]

#3


0  

import operator

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

Finds the coordinate with minimum y, retrieves x.

找到最小y的坐标,检索x。

If you dislike operator.itemgetter, do:

如果您不喜欢operator.itemgetter,请执行以下操作:

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

#1


17  

max(L, key=lambda item: (item[0], -item[1]))

Output:

输出:

[2, 1]

#2


1  

Your request is kind of 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]

#3


0  

import operator

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

Finds the coordinate with minimum y, retrieves x.

找到最小y的坐标,检索x。

If you dislike operator.itemgetter, do:

如果您不喜欢operator.itemgetter,请执行以下操作:

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