[Python-es] Re:_[Python-es]_Ordenación_de_listas

Attila Saorin mostroratas en yahoo.es
Mar Sep 3 16:46:32 CEST 2002


 --- rapto <rapto en arrakis.es> escribió: 

Esteban, yo no se si te hace falta o no optimizar pero en la doc te avisan del uso del sort
pasandole una funcion de comparacion:

The sort() method takes an optional argument specifying a comparison function of two arguments
(list items) which should return a negative, zero or positive number depending on whether the
first argument is considered smaller than, equal to, or larger than the second argument. Note that
this slows the sorting process down considerably; e.g. to sort a list in reverse order it is much
faster to use calls to the methods sort() and reverse() than to use the built-in function sort()
with a comparison function that reverses the ordering of the elements. 

Por si te interesa.

Attila

> Sort toma un argumento función, que debe comportarse como cmp, pero de 
> la manera que quieras.
> 
> a=['Hola', 'Epi', 'Hola', 'Blas']
> 
> def compara(a,b):
>   'ordena por la última letra'
>   return cmp(a[-1],b[-1]) 
> 
> a.sort(compara)
> 
> print a
> 
> #Sale ['Hola', 'Hola', 'Epi', 'Blas']


_______________________________________________________________
Yahoo! Messenger
Nueva versión: Webcam, voz, y mucho más ¡Gratis! 
Descárgalo ya desde http://messenger.yahoo.es


Más información sobre la lista de distribución Python-es