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
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
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
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
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
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]:
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
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
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]]
>
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
10 matches
Mail list logo