numpy 列表/数组嵌套排序的Numba实现

1szpjjfi  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(106)

我尝试在Numba中使用以下函数:

@numba.njit
def nested_sort(s):
    return sorted(s, key=lambda x: (x[1], x[2]))

s = [[1, 3, 11], [2, 3, 19], [3, 2, 18], [4, 2, 9]] 
nested_sort(s)

但是,Numba不支持此功能。我正在寻找这个函数的一个变体,它将在没有Python模式下与Numba一起工作。

alen0pnh

alen0pnh1#

你想要的是np.lexsort。它根据指定的键序列的 * 反向 * 顺序进行排序。This question也可能是相关的。

sorted_s = s[np.lexsort((s[:,2], s[:,1]))]

编辑:你似乎已经编辑了你的问题,你不再寻找一个 numpy 答案。我还是把这个答案留在这里。

相关问题