FAQ
Hi;
i am having trouble trying to sort the rows of a 2 dimensional array by the
values in the first column .. does anybody know how or have an example of
how to do this ??? while leaving the remain columns remain relative to the
leading column

from numpy import *

a=array( [ [4, 4, 3], [4, 5, 2], [3, 1, 1] ] )

i would like to generate the output (or get the output ...)

b = [ [3,1,1], [4,4,3], [4,5,2] ]

thanks;
bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100119/d34b8c45/attachment.htm>

Search Discussions

  • Robert somerville at Jan 19, 2010 at 4:50 pm
    Hi;
    i am having trouble trying to sort the rows of a 2 dimensional array by the
    values in the first column .. does anybody know how or have an example of
    how to do this ??? while leaving the remain columns remain relative to the
    leading column

    from numpy import *

    a=array( [ [4, 4, 3], [4, 5, 2], [3, 1, 1] ] )

    i would like to generate the output (or get the output ...)

    b = [ [3,1,1], [4,4,3], [4,5,2] ]

    thanks;
    bob
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100119/788d9739/attachment.htm>
  • Robert Somerville at Jan 19, 2010 at 5:00 pm
    Hi;
    i am having trouble trying to sort the rows of a 2 dimensional array by
    the values in the first column .. does anybody know how or have an
    example of how to do this ??? while leaving the remain columns remain
    relative to the leading column

    from numpy import *

    a=array( [ [4, 4, 3], [4, 5, 2], [3, 1, 1] ] )

    i would like to generate the output (or get the output ...)

    b = [ [3,1,1], [4,4,3], [4,5,2] ]



    thanks;
    bob
  • Robert Kern at Jan 19, 2010 at 5:39 pm

    On 2010-01-19 11:00 AM, Robert Somerville wrote:
    Hi;
    i am having trouble trying to sort the rows of a 2 dimensional array by
    the values in the first column .. does anybody know how or have an
    example of how to do this ??? while leaving the remain columns remain
    relative to the leading column
    You will want to ask numpy questions on the numpy mailing list:

    http://www.scipy.org/Mailing_Lists
    from numpy import *

    a=array( [ [4, 4, 3], [4, 5, 2], [3, 1, 1] ] )

    i would like to generate the output (or get the output ...)

    b = [ [3,1,1], [4,4,3], [4,5,2] ]
    In [4]: import numpy as np

    In [5]: a = np.array( [ [4, 4, 3], [4, 5, 2], [3, 1, 1] ] )

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

    In [7]: b = a[i]

    In [8]: b
    Out[8]:
    array([[3, 1, 1],
    [4, 4, 3],
    [4, 5, 2]])

    --
    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
  • Stephen Hansen at Jan 19, 2010 at 5:39 pm

    On Tue, Jan 19, 2010 at 9:00 AM, Robert Somerville wrote:

    Hi;
    Hi, why did you post this three times?

    i am having trouble trying to sort the rows of a 2 dimensional array by the
    values in the first column .. does anybody know how or have an example of
    how to do this ??? while leaving the remain columns remain relative to the
    leading column

    from numpy import *

    a=array( [ [4, 4, 3], [4, 5, 2], [3, 1, 1] ] )

    i would like to generate the output (or get the output ...)

    b = [ [3,1,1], [4,4,3], [4,5,2] ]
    I don't use numpy, so this may or may not work. But for a regular python
    list-of-lists (2 dimensional array), you simply do:

    a.sort(key=lambda x: x[0])

    --S
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20100119/eb363920/attachment.htm>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 19, '10 at 4:48p
activeJan 19, '10 at 5:39p
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase