问题
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 lexsort
ing 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