![](https://secure.gravatar.com/avatar/05727ab69d440f21fc847b08b9bbb760.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
On Sat, Aug 1, 2009 at 6:23 AM, Martin<mdekauwe@gmail.com> 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!
If you only want to base the sorting on one column, then you can use argsort on the column and reorder the entire array. If you want to sort by rows then a thread last december 21, 2008 on the numpy discussion list on sortrows would help. (as a windows user, I have no idea what your gawk command means) Josef
Thanks
Martin _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/36b04adcdce59af3ecbdec908fa4158f.jpg?s=120&d=mm&r=g)
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@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/ad13088a623822caf74e635a68a55eae.jpg?s=120&d=mm&r=g)
On Sat, Aug 1, 2009 at 7:47 AM, Emmanuelle Gouillart<emmanuelle.gouillart@normalesup.org> wrote:
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@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
or maybe: sorting rows by the last column (since it is not sorted already)
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)]) ind = np.argsort(a[:,-1]) a[ind,:] array([[ 1, 11, 2, 0], [ 1, 5, 6, 2], [ 1, 3, 2, 3], [ 1, 10, 1, 3], [ 1, 9, 0, 4], [ 1, 0, 2, 5], [ 1, 4, 4, 6], [ 1, 8, 9, 6], [ 1, 2, 8, 7]])
Josef
![](https://secure.gravatar.com/avatar/fa7b17875148edbfb3a07d6b6223601d.jpg?s=120&d=mm&r=g)
Martin <mdekauwe <at> gmail.com> writes:
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
I think lexsort does what you need. Get the sorted row indices:
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]]) ind = np.lexsort(a.T[::-1])
a.T gives an array of columns, and reversing them with ::-1 sorts by the first column values, then the second column values if the first column values are equal, and so on. Now index the the original array:
ind array([0, 2, 4, 3, 1, 8, 7, 6, 5]) a[ind] array([[ 1, 0, 2, 5], [ 1, 2, 8, 7], [ 1, 3, 2, 3], [ 1, 4, 4, 6], [ 1, 5, 6, 2], [ 1, 8, 9, 6], [ 1, 9, 0, 4], [ 1, 10, 1, 3], [ 1, 11, 2, 0]])
Neil
![](https://secure.gravatar.com/avatar/05727ab69d440f21fc847b08b9bbb760.jpg?s=120&d=mm&r=g)
Hi all, Thanks for the replies...perhaps if I explained the context it might help (or it might not!). Basically it is an image processing problem so the array is rows, columns. So I want to sort the array such that row col 1 1 1 2 1 3 etc 2 1 2 2 2 3 then I can use the other columns to create images safe in the knowledge they are in the correct row, column 2D space. I think Emmanuelle solution does the trick from what I can see, I am just trying it out. Although I have never seen [:,:2] what does the :2 bit mean? Neil your solution results in column 4 changing some of the ordering from what I can see (the zero in the fourth column). Thanks a lot for the suggestions Martin On Aug 1, 11:26 pm, Neil Crighton <neilcrigh...@gmail.com> wrote:
Martin <mdekauwe <at> gmail.com> writes:
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
I think lexsort does what you need. Get the sorted row indices:
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]])
ind = np.lexsort(a.T[::-1])
a.T gives an array of columns, and reversing them with ::-1 sorts by the first column values, then the second column values if the first column values are equal, and so on. Now index the the original array:
ind
array([0, 2, 4, 3, 1, 8, 7, 6, 5])>>> a[ind]
array([[ 1, 0, 2, 5], [ 1, 2, 8, 7], [ 1, 3, 2, 3], [ 1, 4, 4, 6], [ 1, 5, 6, 2], [ 1, 8, 9, 6], [ 1, 9, 0, 4], [ 1, 10, 1, 3], [ 1, 11, 2, 0]])
Neil
_______________________________________________ SciPy-User mailing list SciPy-U...@scipy.orghttp://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/b684c02bab6c8d54c0c25c4b69ee1135.jpg?s=120&d=mm&r=g)
On 1-Aug-09, at 7:31 PM, Martin wrote:
Although I have never seen [:,:2] what does the :2 bit mean?
Up to, but not including, the third column of a 2D array. In [6]: x = arange(16).reshape(4,4) In [7]: x Out[7]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) In [8]: x[:,:2] Out[8]: array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]])
![](https://secure.gravatar.com/avatar/05727ab69d440f21fc847b08b9bbb760.jpg?s=120&d=mm&r=g)
Thanks David - that is very useful. On Aug 2, 7:13 am, David Warde-Farley <d...@cs.toronto.edu> wrote:
On 1-Aug-09, at 7:31 PM, Martin wrote:
Although I have never seen [:,:2] what does the :2 bit mean?
Up to, but not including, the third column of a 2D array.
In [6]: x = arange(16).reshape(4,4)
In [7]: x Out[7]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])
In [8]: x[:,:2] Out[8]: array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]])
_______________________________________________ SciPy-User mailing list SciPy-U...@scipy.orghttp://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/fa7b17875148edbfb3a07d6b6223601d.jpg?s=120&d=mm&r=g)
Martin <mdekauwe <at> gmail.com> writes:
Hi all,
Thanks for the replies...perhaps if I explained the context it might help (or it might not!). Basically it is an image processing problem so the array is rows, columns. So I want to sort the array such that ... Neil your solution results in column 4 changing some of the ordering from what I can see (the zero in the fourth column).
I think I see now - you want the values in the third and fourth columns to be unchanged after sorting? Then, yeah, I think Emmanuelle's version is the way to go. I was confused by the first sentence in your original post. To me, 'sort an array by the first column' means sort every column using the ordering of values in the first column. Neil
![](https://secure.gravatar.com/avatar/05727ab69d440f21fc847b08b9bbb760.jpg?s=120&d=mm&r=g)
Apologies Neil! Your solution was in fact exactly what I wanted...I don't know why I said otherwise yesterday (tiredness). As you said what I wanted to do was to sort by the first and then second cols, moving the third and fourth columns to their respective places. What I didn't want was for these cols(3 and 4) to also be numerically sorted. I am trying it now. Thanks On Aug 2, 9:43 am, Neil Crighton <neilcrigh...@gmail.com> wrote:
Martin <mdekauwe <at> gmail.com> writes:
Hi all,
Thanks for the replies...perhaps if I explained the context it might help (or it might not!). Basically it is an image processing problem so the array is rows, columns. So I want to sort the array such that ... Neil your solution results in column 4 changing some of the ordering from what I can see (the zero in the fourth column).
I think I see now - you want the values in the third and fourth columns to be unchanged after sorting? Then, yeah, I think Emmanuelle's version is the way to go.
I was confused by the first sentence in your original post. To me, 'sort an array by the first column' means sort every column using the ordering of values in the first column.
Neil
_______________________________________________ SciPy-User mailing list SciPy-U...@scipy.orghttp://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/05727ab69d440f21fc847b08b9bbb760.jpg?s=120&d=mm&r=g)
Works like a treat thanks Neil. On Aug 2, 2:11 pm, Martin <mdeka...@gmail.com> wrote:
Apologies Neil! Your solution was in fact exactly what I wanted...I don't know why I said otherwise yesterday (tiredness). As you said what I wanted to do was to sort by the first and then second cols, moving the third and fourth columns to their respective places. What I didn't want was for these cols(3 and 4) to also be numerically sorted.
I am trying it now.
Thanks
On Aug 2, 9:43 am, Neil Crighton <neilcrigh...@gmail.com> wrote:
Martin <mdekauwe <at> gmail.com> writes:
Hi all,
Thanks for the replies...perhaps if I explained the context it might help (or it might not!). Basically it is an image processing problem so the array is rows, columns. So I want to sort the array such that ... Neil your solution results in column 4 changing some of the ordering from what I can see (the zero in the fourth column).
I think I see now - you want the values in the third and fourth columns to be unchanged after sorting? Then, yeah, I think Emmanuelle's version is the way to go.
I was confused by the first sentence in your original post. To me, 'sort an array by the first column' means sort every column using the ordering of values in the first column.
Neil
_______________________________________________ SciPy-User mailing list SciPy-U...@scipy.orghttp://mail.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-User mailing list SciPy-U...@scipy.orghttp://mail.scipy.org/mailman/listinfo/scipy-user
participants (5)
-
David Warde-Farley
-
Emmanuelle Gouillart
-
josef.pktd@gmail.com
-
Martin
-
Neil Crighton