I'm relatively new at numpy and I want to sort an array to find a point which has the lowest x and y values.
How would I sort the following array in numpy, such that it sorts the first column in ascending order, which are the x coordinate values, and then the second column, which are the y coordinates?
[[0.1, 0.2]
[0.5, 0.1]
[0.3, 0.9]
[0.7, 1.0]
[0.1, 0.3]
[0.2, 0.3]]
I have seen answers regarding numpy.sort, but I did not understand what the meaning of sorting by an axis was.
You can use numpy.lexsort
. This performs an indirect lexicographic sort. For some reason it sorts the last row first, so in your case we have to take the transpose and reverse the order of x
and y
.
>>> A
array([[0.1, 0.2],
[0.5, 0.1],
[0.3, 0.9],
[0.7, 1. ],
[0.1, 0.3],
[0.2, 0.3]])
>>> A[np.lexsort(A.T[::-1])]
array([[0.1, 0.2],
[0.1, 0.3],
[0.2, 0.3],
[0.3, 0.9],
[0.5, 0.1],
[0.7, 1. ]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.