Re: Sorting Multidimesional array(newbie)

2006-12-14 Thread Fredrik Lundh
Brian Mills wrote: > >> but using a compare function instead of a key mapper is not good advice, >> in general. brief discussion here: >> http://effbot.org/pyfaq/i-want-to-do-a-complicated-sort-can-you-do-a-schwartzian-transform-in-python > > Is this mostly because of the stability problem descr

Re: Sorting Multidimesional array(newbie)

2006-12-14 Thread Peter Otten
Brian Mills wrote: >> but using a compare function instead of a key mapper is not good advice, >> in general. brief discussion here: >> http://effbot.org/pyfaq/i-want-to-do-a-complicated-sort-can-you-do-a-schwartzian-transform-in-python > Is this mostly because of the stability problem described

Re: Sorting Multidimesional array(newbie)

2006-12-14 Thread Brian Mills
Fredrik Lundh wrote: > Brian Mills wrote: > > > There's another (IMHO more readable) way to do it if you can afford > > defining a short little "compare" function, and telling .sort() > > to use that instead of its default: > > > def myListCmp(lst1, lst2): > > ... if lst1[0] < lst2[0]: ret

Re: Sorting Multidimesional array(newbie)

2006-12-12 Thread Travis E. Oliphant
Tartifola wrote: > Hi, > how can I sort an array like > > array([[5, 2], >[1, 3]]) > > along the first column to obtain > > array([[1, 3], >[5, 2]]) > i.e. keeping track of the ordered couples? > > Thanks, > A Just to add one more solution to this question that works with numpy

Re: Sorting Multidimesional array(newbie)

2006-12-12 Thread Robert Kern
Fredrik Lundh wrote: > also note the OP didn't specify what to do for records where the first > column was identical, so I guess a plain sort() call, without any custom > compares or mappings, would work as well as the fancier alternatives... If the OP had lists of lists, yes. However, he seems

Re: Sorting Multidimesional array(newbie)

2006-12-11 Thread Fredrik Lundh
Brian Mills wrote: > There's another (IMHO more readable) way to do it if you can afford > defining a short little "compare" function, and telling .sort() > to use that instead of its default: > def myListCmp(lst1, lst2): > ... if lst1[0] < lst2[0]: return -1 > ... if lst2[0] > lst2[0]:

Re: Sorting Multidimesional array(newbie)

2006-12-11 Thread Brian Mills
There's another (IMHO more readable) way to do it if you can afford defining a short little "compare" function, and telling .sort() to use that instead of its default: >>> def myListCmp(lst1, lst2): ... if lst1[0] < lst2[0]: return -1 ... if lst2[0] > lst2[0]: return 1 ... return 0 ... >>> a

Re: Sorting Multidimesional array(newbie)

2006-12-11 Thread Peter Otten
Matimus wrote: > Tartifola wrote: >> Hi, >> how can I sort an array like >> >> array([[5, 2], >>[1, 3]]) >> >> along the first column to obtain >> >> array([[1, 3], >>[5, 2]]) >> i.e. keeping track of the ordered couples? >> >> Thanks, >> A > > use a sort key: > from operator

Re: Sorting Multidimesional array(newbie)

2006-12-11 Thread Matimus
Tartifola wrote: > Hi, > how can I sort an array like > > array([[5, 2], >[1, 3]]) > > along the first column to obtain > > array([[1, 3], >[5, 2]]) > i.e. keeping track of the ordered couples? > > Thanks, > A use a sort key: >>>from operators import getitem >>>a = [[5,2],[1,3]] >

Sorting Multidimesional array(newbie)

2006-12-11 Thread Tartifola
Hi, how can I sort an array like array([[5, 2], [1, 3]]) along the first column to obtain array([[1, 3], [5, 2]]) i.e. keeping track of the ordered couples? Thanks, A -- http://mail.python.org/mailman/listinfo/python-list