Re: Sorting a multidimensional array by multiple keys

2007-04-02 Thread Alex Martelli
Steven Bethard <[EMAIL PROTECTED]> wrote: > Thomas Krüger wrote: > > Alex Martelli schrieb: > >> Thomas Krüger <[EMAIL PROTECTED]> wrote: > >>> def sorter(a, b): > >>> return cmp(a.id, b.id) > >>> > >>> obj_lst.sort(sorter) > >> A MUCH better way to obtain exactly the same semantics would be:

Re: Sorting a multidimensional array by multiple keys

2007-04-02 Thread bearophileHUGS
Steven Bethard: > there's almost never a reason to use the cmp= argument to > sort() anymore. It's almost always better to use the key= argument. I always use key now, but maybe cmp uses less memory. There can be few situations where cmp is better still. Bye, bearophile -- http://mail.python.o

Re: Sorting a multidimensional array by multiple keys

2007-04-02 Thread Steven Bethard
Thomas Krüger wrote: > Alex Martelli schrieb: >> Thomas Krüger <[EMAIL PROTECTED]> wrote: >>> def sorter(a, b): >>> return cmp(a.id, b.id) >>> >>> obj_lst.sort(sorter) >> A MUCH better way to obtain exactly the same semantics would be: >> >> def getid(a): >> return a.id >> >> obj_list.sort(

Re: Sorting a multidimensional array by multiple keys

2007-04-01 Thread Thomas Krüger
Alex Martelli schrieb: > Thomas Krüger <[EMAIL PROTECTED]> wrote: >> def sorter(a, b): >> return cmp(a.id, b.id) >> >> obj_lst.sort(sorter) > > A MUCH better way to obtain exactly the same semantics would be: > > def getid(a): > return a.id > > obj_list.sort(key=getid) Frankly speaking

Re: Sorting a multidimensional array by multiple keys

2007-04-01 Thread Alex Martelli
Thomas Krüger <[EMAIL PROTECTED]> wrote: > Rehceb Rotkiv schrieb: > > can I sort a multidimensional array in Python by multiple sort keys? A > > litte code sample would be nice! > > You can pass a function as argument to the sort method of a list. > The function should take two arguments and retu

Re: Sorting a multidimensional array by multiple keys

2007-04-01 Thread Paulo da Silva
Rehceb Rotkiv escreveu: > Hello everyone, > > can I sort a multidimensional array in Python by multiple sort keys? A > litte code sample would be nice! class MyList(list): # This is the index of the element to be compared CmpIndex=0 # Comparision methods @staticm

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Duncan Booth
Peter Otten <[EMAIL PROTECTED]> wrote: > Duncan Booth wrote: > > from operator import itemgetter > data.sort(key=itemgetter(0)) > data.sort(key=itemgetter(1)) > data.sort(key=itemgetter(4)) > data.sort(key=itemgetter(3)) > > Or, in Python 2.5: > data.sort(key=itemgetter

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Rehceb Rotkiv
Thank you all for your helpful solutions! Regards, Rehceb -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Peter Otten
Duncan Booth wrote: from operator import itemgetter data.sort(key=itemgetter(0)) data.sort(key=itemgetter(1)) data.sort(key=itemgetter(4)) data.sort(key=itemgetter(3)) Or, in Python 2.5: >>> data.sort(key=itemgetter(3, 4, 1, 0)) Peter -- http://mail.python.org/mailman/

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread attn . steven . kuo
On Mar 31, 6:42 am, Rehceb Rotkiv <[EMAIL PROTECTED]> wrote: (snipped) > As each line consists of 5 words, I would break up the data into an array > of five-field-arrays (Would you use lists or tuples or a combination in > Python?). The word "BUT" would be in the middle, with two fields/words > l

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Duncan Booth
Rehceb Rotkiv <[EMAIL PROTECTED]> wrote: > Wait, I made a mistake. The correct result would be > > reaction is BUT by the > pattern , BUT it is > rapid , BUT it is > sea , BUT it is > sodium , BUT it is > this manner BUT the dissolved > > because "by the" comes before and "the dissolved" after "

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Steven Bethard
Rehceb Rotkiv wrote: >> If you want a good answer you have to give me/us more details, and an >> example too. > > OK, here is some example data: > > reaction is BUT by the > sodium , BUT it is > sea , BUT it is > this manner BUT the dissolved > pattern , BUT it is > rapid , BUT it is > > As each

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Rehceb Rotkiv
Wait, I made a mistake. The correct result would be reaction is BUT by the pattern , BUT it is rapid , BUT it is sea , BUT it is sodium , BUT it is this manner BUT the dissolved because "by the" comes before and "the dissolved" after "it is". Sorry for the confusion. -- http://mail.python.org/m

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Rehceb Rotkiv
> If you want a good answer you have to give me/us more details, and an > example too. OK, here is some example data: reaction is BUT by the sodium , BUT it is sea , BUT it is this manner BUT the dissolved pattern , BUT it is rapid , BUT it is As each line consists of 5 words, I would break up t

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread bearophileHUGS
Rehceb Rotkiv: > can I sort a multidimensional array in Python by multiple sort keys? A > litte code sample would be nice! If you want a good answer you have to give me/us more details, and an example too. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Thomas Krüger
Rehceb Rotkiv schrieb: > can I sort a multidimensional array in Python by multiple sort keys? A > litte code sample would be nice! You can pass a function as argument to the sort method of a list. The function should take two arguments and return -1, 0 or 1 as comparison result. Just like the cmp

Sorting a multidimensional array by multiple keys

2007-03-31 Thread Rehceb Rotkiv
Hello everyone, can I sort a multidimensional array in Python by multiple sort keys? A litte code sample would be nice! Thx, Rehceb -- http://mail.python.org/mailman/listinfo/python-list