471,319 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Sorting array



Hi,
I'm working with numerical array and I'm a little lost on a particular
sorting of one of them. In particular I have an array like

a = array([[8,4,1],[2,0,9]])

and I need to sort it using only the first column as reference but
keeping the lines together so to obtain

array([[2, 0, 9],
[8, 4, 1]])

Any help?
Thanks
Nov 30 '07 #1
5 2022
the list.sort method seems to do exactly what you want?

Unless your array() method is creating a custom array object with
different sort functionality than list?

Cheers
Nov 30 '07 #2
Chris Hulan wrote:
the list.sort method seems to do exactly what you want?

Unless your array() method is creating a custom array object with
different sort functionality than list?
Presumably he is using numpy arrays.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 30 '07 #3
Tartifola wrote:
>
Hi,
I'm working with numerical array and I'm a little lost on a particular
sorting of one of them. In particular I have an array like

a = array([[8,4,1],[2,0,9]])

and I need to sort it using only the first column as reference but
keeping the lines together so to obtain

array([[2, 0, 9],
[8, 4, 1]])
In [1]: from numpy import *

In [2]: a = array([[8,4,1],[2,0,9]])

In [3]: i = argsort(a[:,0])

In [4]: a[i]
Out[4]:
array([[2, 0, 9],
[8, 4, 1]])
numpy questions are best asked on the numpy-discussion mailing list since
everyone there automatically knows that you are talking about numpy arrays and
not just misnaming lists. ;-)

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 30 '07 #4


Hi,

On Fri, 30 Nov 2007 14:55:08 -0600
Robert Kern <ro*********@gmail.comwrote:
Tartifola wrote:

Hi,
I'm working with numerical array and I'm a little lost on a particular
sorting of one of them. In particular I have an array like

a = array([[8,4,1],[2,0,9]])

and I need to sort it using only the first column as reference but
keeping the lines together so to obtain

array([[2, 0, 9],
[8, 4, 1]])

In [1]: from numpy import *

In [2]: a = array([[8,4,1],[2,0,9]])

In [3]: i = argsort(a[:,0])

In [4]: a[i]
Out[4]:
array([[2, 0, 9],
[8, 4, 1]])
numpy questions are best asked on the numpy-discussion mailing list since
everyone there automatically knows that you are talking about numpy arrays and
not just misnaming lists. ;-)

http://www.scipy.org/Mailing_Lists
thanks a lot for your help and sorry for using the wrong list.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Dec 1 '07 #5
Tartifola wrote:
>
Hi,

On Fri, 30 Nov 2007 14:55:08 -0600
Robert Kern <ro*********@gmail.comwrote:
>numpy questions are best asked on the numpy-discussion mailing list since
everyone there automatically knows that you are talking about numpy arrays and
not just misnaming lists. ;-)

http://www.scipy.org/Mailing_Lists

thanks a lot for your help and sorry for using the wrong list.
No need to apologize. This isn't the wrong list; it's just that there is a
better list. :-)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Dec 1 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Paul Kirby | last post: by
7 posts views Thread by Federico G. Babelis | last post: by
3 posts views Thread by SilverWolf | last post: by
7 posts views Thread by Foodbank | last post: by
7 posts views Thread by Kamal | last post: by
5 posts views Thread by lemlimlee | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.