[B,IX] = sort(A,...) - Order for sort()-function
Maric Michaud
maric at aristote.info
Tue May 29 11:34:27 EDT 2007
Orlando Döhring a écrit :
>
...
> A = [ 3 7 5
> 0 4 2 ];
>
> # in Python: A = [[3,7,5],[0,4,2]]
>
> [B,IX] = sort(A,2)
>
> # sort by rows
>
> B =
> 3 5 7
> 0 2 4
>
> IX =
> 1 3 2
> 1 3 2
>
> # first line: 3 was formerly in the first position, 5 formerly in
> position 3, 7 formerly in position 2
> # second line: similiarly
>
>
Like this :
In [122]: l=[[3,2,1], [4,6,5,0]]
In [123]: from operator import itemgetter
In [124]: [ list(sorted(enumerate(e), key=itemgetter(1))) for e in l ]
Out[124]: [[(2, 1), (1, 2), (0, 3)], [(3, 0), (0, 4), (2, 5), (1, 6)]]
except the old position is the first element of each tuple, you may have
the same order with this one :
In [126]: [ list((pos, elt) for pos, elt in sorted(enumerate(e),
key=itemgetter(1))) for e in l ]
Out[126]: [[(1, 2), (2, 1), (3, 0)], [(0, 3), (4, 0), (5, 2), (6, 1)]]
More information about the Python-list
mailing list