[SciPy-User] sorting an array

Emmanuelle Gouillart emmanuelle.gouillart at normalesup.org
Sat Aug 1 07:47:08 EDT 2009


	Hi Martin,

	I'm not sure if I understand what you mean, but does the
following solve your problem?

>>> a = np.array([(1,0,2,5),(1,5,6,2),(1,2,8,7),(1,4,4,6),(1,3,2,3),(1,11,2,0),(1,10,1,3),(1,9,0,4),(1,8,9,6)])
>>> a
array([[ 1,  0,  2,  5],
       [ 1,  5,  6,  2],
       [ 1,  2,  8,  7],
       [ 1,  4,  4,  6],
       [ 1,  3,  2,  3],
       [ 1, 11,  2,  0],
       [ 1, 10,  1,  3],
       [ 1,  9,  0,  4],
       [ 1,  8,  9,  6]])
>>> a[:,:2] = np.sort(a[:,:2], axis=0)
>>> a
array([[ 1,  0,  2,  5],
       [ 1,  2,  6,  2],
       [ 1,  3,  8,  7],
       [ 1,  4,  4,  6],
       [ 1,  5,  2,  3],
       [ 1,  8,  2,  0],
       [ 1,  9,  1,  3],
       [ 1, 10,  0,  4],
       [ 1, 11,  9,  6]])

	Cheers,

	Emmanuelle

On Sat, Aug 01, 2009 at 03:23:47AM -0700, Martin wrote:
> Hi,

> I would ideally like to sort an multidimensional array by  the first
> column, but I can only manage to adjust the whole array. For
> example...

> >a = array([(1,0,2,5),(1,5,6,2),(1,2,8,7),(1,4,4,6),(1,3,2,3),(1,11,2,0),(1,10,1,3),(1,9,0,4),(1,8,9,6)])
> >a
> array([[ 1,  0,  2,  5],
>        [ 1,  5,  6,  2],
>        [ 1,  2,  8,  7],
>        [ 1,  4,  4,  6],
>        [ 1,  3,  2,  3],
>        [ 1, 11,  2,  0],
>        [ 1, 10,  1,  3],
>        [ 1,  9,  0,  4],
>        [ 1,  8,  9,  6]])
> >sort(a, axis = 0)
> array([[ 1,  0,  0,  0],
>        [ 1,  2,  1,  2],
>        [ 1,  3,  2,  3],
>        [ 1,  4,  2,  3],
>        [ 1,  5,  2,  4],
>        [ 1,  8,  4,  5],
>        [ 1,  9,  6,  6],
>        [ 1, 10,  8,  6],
>        [ 1, 11,  9,  7]])

> When really what i wanted was not for the 3rd and 4th columns to also
> be sorted nunmerically in that way. In unix I would reformat the first
> two columns in gawk i.e. gawk '{printf("%2.3d %2.3d, %d, %d\n", $1,
> $2, $3, $4)}' | sort -n -k 1

> Any help would be appreciated!

> Thanks

> Martin
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-User mailing list