Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

= sort(A,...) - Order for sort()-function


Dear community,

I want to use the sort function to sort a (nested) list. General information can be found below.

http://www.python.org/doc/2.4.2/lib/typesseq-mutable.html
http://wiki.python.org/moin/HowTo/Sorting
http://www.python.org/doc/2.4.4/whatsnew/node12.html

I want to solve the following problem. Given a list I do not only want to retrieve the sorted list but also the position of the original elements (IX below). The example is taken from Matlab syntax:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html

'[B,IX] = sort(A,...) also returns an array of indices IX, where size(IX) == size(A). If A is a vector, B = A(IX). If A is an m-by-n matrix, then each column of IX is a permutation vector of the corresponding column of A, such that
for j = 1:n
    B(:,j) = A(IX(:,j),j);
end'
--

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

Yours,

Orlando

--
Psssst! Schon vom neuen GMX MultiMessenger gehrt?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger

Orlando Dhring a crit :

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)]]

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc