At Wednesday 27/9/2006 08:51, Steve Holden wrote:

If you want the indexes as well you need to become a bit tricky.
Remember that enumerate() will produced two-element tuples with the
index value associated with the list value.

  >>> [x for x in enumerate(a)]
[(0, 9), (1, 4), (2, 3), (3, 5), (4, 2), (5, 6), (6, 7), (7, 1), (8, 2)]

But you really want the index second, so the sort works on the list
values not the index values. Combining all this we get

  >>> sorted((x[1], x[0]) for x in enumerate(a))
[(1, 7), (2, 4), (2, 8), (3, 2), (4, 1), (5, 3), (6, 5), (7, 6), (9, 0)]

Why forcing to use enumerate if it doesn't fit? And a generator won't help here since you have to access all the items.

sorted([(a[i],i) for i in range(len(a))])

If you want the ordered results and the indices exactly like in the matlab example:

> [y,i] = sort(x);
y, i = zip(*sorted([(a[i],i) for i in range(len(a))]))



Gabriel Genellina
Softlab SRL

        
        
                
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! http://www.yahoo.com.ar/respuestas

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to