python - How to create a this Matrix by Numpy , and sort the matrix at second row? -
i want create matrix numpy follow:
the first column composed 0 , 1 , in case , value not important second column composed integer , want sort matrix column.
[[[1,1,1,1,1] , [3]], [[0,0,0,0,0] , [2]], [[1,1,1,1,1] , [5]]]
the question is: - how create matrix that? - after created , how sort matrix second column
and answer follow:
[ [[0,0,0,0,0] , [2]], [[1,1,1,1,1] , [3]], [[1,1,1,1,1] , [5]] ]
i'm fresh in numpy, tried lot didn't succeeded, please help.
in following, used newlines format code clarity putting matrix rows onto separate lines. not necessary, feel helps show content of each row (basically list 5 items , list 1 item).
when defining numpy array, can define datatype (dtype) both field name , type of data stored in field. lists considered object
type numpy, each row, assigned field name each item in row ('x' , 'y') , assigned data type of object
.
n = np.array([ ([1,1,1,1,1] , [3]), ([0,0,0,0,0] , [2]), ([0,0,0,0,0] , [5])], dtype=[('x', object),('y', object)]
numpy arrays have builtin method called .sort()
enables sort rows. if give order argument using name of field, sort field (i.e. column). in case, sort 'y' field/column.
n.sort(order='y')
Comments
Post a Comment