如何对列表列表进行排序,并仅保留每个第一个元素的最大第二个元素? [英] How to sort a list of lists and and to keep only the maximal 2nd element of each of the 1st elements?
问题描述
假设我有一些列表:
lst = [[2,6],[1,4],[0,1],[1,1],[2,3],[0,2]]
我想按第一个元素对第一进行排序,并且对于每个子列表,当按第一个元素分组时,请保留具有最大第二个元素的子列表.
I want to sort lst by the first element and for each sublist keep the one with the maximal second element when grouped by the first element.
结果将是:
results
>>> [[0,2],[1,4],[2,6]]
有人可以帮助我吗?
推荐答案
您可以使用 np.maximum.reduceat
:
import numpy as np
lst = np.array([[2,6],[1,4],[0,1],[1,1],[2,3],[0,2]])
lst = lst[np.argsort(lst[:,0])] #sorting lst by first row
u, idx = np.unique(lst[:,0], return_index = True)
print(np.c_[u, np.maximum.reduceat(lst[:,1], idx)])
首先应该对数组进行排序.然后,您需要获取将数组分成几组的索引: idx = [0,2,4]
和第一列的相应值 u = [0,1,2]
.最后,使用 np.maximum.reduceat
以获得以指定的索引 idx
开头的组的最大值,并正确地将其显示为 u
At first array should be sorted. Then you need to get indices that splits array into groups: idx = [0, 2, 4]
and corresponding values of first column u = [0, 1, 2]
. Finally, use np.maximum.reduceat
in order to get maximum values of groups that starts at indices idx
specified and display it concatenated rightwise to u
.
备注: 在这里我使用了 numpy
,这是一个广泛使用的库,它允许将循环推送到C级,这要快得多.纯粹的pythonic解决方案也值得关注.
Remark: I used numpy
here, a widely used library that allows to push looping into C level which is much faster. Purely pythonic solutions are worth attention too.
奖金: 实际上,这是一个使用 numpy_indexed
库(未广泛使用)的单行代码,该库专用于数组的分组操作:
Bonus: This is actually a one liner using a numpy_indexed
library (not so widely used) dedicated for groupby operations of arrays:
import numpy_indexed as npi
import numpy as np
np.transpose(npi.group_by(lst[:, 0]).max(lst[:, 1]))
这篇关于如何对列表列表进行排序,并仅保留每个第一个元素的最大第二个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!