Sorting array

Discussion in 'Python' started by Tartifola, Nov 30, 2007.

  1. Tartifola

    Tartifola Guest

    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
     
    Tartifola, Nov 30, 2007
    #1
    1. Advertisements

  2. Tartifola

    Chris Hulan Guest

    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
     
    Chris Hulan, Nov 30, 2007
    #2
    1. Advertisements

  3. Tartifola

    Robert Kern Guest

    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
     
    Robert Kern, Nov 30, 2007
    #3
  4. Tartifola

    Robert Kern Guest

    In [1]: from numpy import *

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

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

    In [4]: a
    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
     
    Robert Kern, Nov 30, 2007
    #4
  5. Tartifola

    Tartifola Guest

    Hi,



    thanks a lot for your help and sorry for using the wrong list.
     
    Tartifola, Dec 1, 2007
    #5
  6. Tartifola

    Robert Kern Guest

    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
     
    Robert Kern, Dec 1, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.