基于乱序比较将列表与min()进行比较(Comparing lists with min() based on out of order comparisons)

我有一个列表列表,例如q = [[1,2],[3,4]] ,其中每个子列表是2个整数的列表,我返回每个极值点的索引(我想? )。

我需要的是列表的索引,在子列表的所有第二个条目中的第二个条目中具有最小/最大值,并且在第二个条目中存在具有相同值的其他子列表,具有min /的索引返回最小/最大秒值条目列表中的最大第一个值。

例如,如果q = [[1, 2], [3, 4], [1, 0], [0, 5]] ,我需要最小秒,如果是平局,那么最小秒,然后是第一。 所以我需要min(S)返回[1,0] 。 相反,它似乎返回的是[0,5] 。

>>> q = [[1,2],[3,4]] >>> min(q) [1, 2] >>> q.append([1,0]) >>> min(q) [1, 0] >>> q.append([0,5]) >>> min(q) [0, 5] >>> q [[1, 2], [3, 4], [1, 0], [0, 5]]

根据这里的答案 ,比较列表按照元素的顺序对它们进行比较,使用下一个用于打破连接器的列表条目。

>>> q.append([0,6]) >>> q.append([0,4]) >>> min(q) [0, 4] >>> q [[1, 2], [3, 4], [1, 0], [0, 5], [0, 6], [0, 4]] >>>

有什么方法可以控制比较中的顺序吗? 我试着阅读文档 ,但我不明白我在读什么。

I have a list of lists, for example q = [[1,2],[3,4]], where each sub-list is a list of 2 integers, and I return the index of each extreme point (I think?).

What I need is the index of the list with the min/max value in the second entry out of all the second entries of the sublists, and where there are other sublists with the same value in the second entry, the index with the min/max first value out of the list of min/max second value entries is returned.

For example, if q = [[1, 2], [3, 4], [1, 0], [0, 5]], and I need min second, if tie, then min second and then first. So I need min(S) to return [1,0]. Instead, what it seems to return is [0,5].

>>> q = [[1,2],[3,4]] >>> min(q) [1, 2] >>> q.append([1,0]) >>> min(q) [1, 0] >>> q.append([0,5]) >>> min(q) [0, 5] >>> q [[1, 2], [3, 4], [1, 0], [0, 5]]

According to this answer here, comparing lists compares them in order of the elements, using the next list entry for tie-breakers.

>>> q.append([0,6]) >>> q.append([0,4]) >>> min(q) [0, 4] >>> q [[1, 2], [3, 4], [1, 0], [0, 5], [0, 6], [0, 4]] >>>

Is there some way I can control the ordering in the comparison? I tried reading through the documentation, but I don't understand what I'm reading.

最满意答案

这对你有用吗?

min(q, key = lambda x: (x[1],x[0]))

Does this work for you?

min(q, key = lambda x: (x[1],x[0]))

更多推荐