sort 2d numpy array lexicographically

我是研究僧i 提交于 2020-01-22 20:54:06

问题


I have a large 2d array with hundreds of columns. I would like to sort it lexicographically, i.e. by first column, then by second column, and so on until the last column. I imagine this should be easy to do but I haven't been able to find a quick way to do this.


回答1:


This is what numpy.lexsort is for, but the interface is awkward. Pass it a 2D array, and it will argsort the columns, sorting by the last row first, then the second-to-last row, continuing up to the first row:

>>> x
array([[0, 0, 0, 2, 3],
       [2, 3, 2, 3, 2],
       [3, 1, 3, 0, 0],
       [3, 1, 1, 3, 1]])
>>> numpy.lexsort(x)
array([4, 1, 2, 3, 0], dtype=int64)

If you want to sort by rows, with the first column as the primary key, you need to rotate the array before lexsorting it:

>>> x[numpy.lexsort(numpy.rot90(x))]
array([[0, 0, 0, 2, 3],
       [2, 3, 2, 3, 2],
       [3, 1, 1, 3, 1],
       [3, 1, 3, 0, 0]])


来源:https://stackoverflow.com/questions/38277143/sort-2d-numpy-array-lexicographically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!