Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lexicographic sort float array python

Okay, so, I have a 4x2 numpy ndarray, and I want to sort it lexicographically. That is, if I have the array

[[0,0],
[1,1],
[0,1],
[1,0]]

I want it to become

[[0,0],
[0,1],
[1,0],
[1,1]]

How do I do this?

like image 423
Pedro Carvalho Avatar asked Jul 24 '26 21:07

Pedro Carvalho


1 Answers

You can use numpy's lexsort. Lexsort, though, sorts using the last column as the primary key. One way to get what you want is to specify the columns explicitly:

 x[np.lexsort((x[:,1], x[:,0]))]

 # array([[0, 0],
 #   [0, 1],
 #   [1, 0],
 #   [1, 1]])
like image 154
tom10 Avatar answered Jul 26 '26 14:07

tom10