本文介绍了笨拙的字典顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按字典顺序对以下数组 a 进行排序(获取索引位置),但是,我在理解numpy结果时遇到了问题:
I'd like to lexicographically sort the following array a (get index positions), but, I'm having problems understanding the numpy results:
>>> a = np.asarray([[1, 1, 1, 2, 1, 2], [2, 1, 2, 3, 1, 0], [1, 2, 3, 3, 2, 2]])
>>> a
array([[1, 1, 1, 2, 1, 2],
[2, 1, 2, 3, 1, 0],
[1, 2, 3, 3, 2, 2]])
>>> np.lexsort(a)
array([0, 5, 1, 4, 2, 3])
例如,我不明白为什么[1, 2, 1]
(a[:,0]
)是排序索引0而[1, 1, 2]
(a[:,1]
)是索引5,甚至认为它应该比[1, 2, 1]
还要小. /p>
For instance, I don't understand why [1, 2, 1]
(a[:,0]
) is sort-index 0 while [1, 1, 2]
(a[:,1]
) is index 5, even thought it should be samller than [1, 2, 1]
.
推荐答案
键的重要性顺序与您期望的相反.为了获得预期的结果,只需将矩阵上下颠倒
The order of significance for keys is opposite to what you expected.In order to get expected result just flip the matrix upside down
>>> np.lexsort(np.flipud(a))
array([1, 4, 0, 2, 5, 3])
这篇关于笨拙的字典顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!