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:
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
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(
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
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
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
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
Thank you all for your helpful solutions!
Regards,
Rehceb
--
http://mail.python.org/mailman/listinfo/python-list
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/
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
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 "
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
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
> 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
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
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
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
17 matches
Mail list logo