How to sort list

Fredrik Lundh fredrik at pythonware.com
Wed Nov 22 10:13:50 CET 2006


Klaus Alexander Seistrup wrote:

> Decorate-sort-undecorate?
> 
> #v+
> 
> array = []
> 
> for addr in Emails:
>   (user, domain) = addr.split('@')
>   array.append((domain, user, addr))
> # end for
> 
> array.sort()
> 
> SortedEmails = [addr for (user, domain, addr) in array]
> 
> #v-

note that DSU is built into Python these days:

     L.sort(key=transform)

so you could use e.g.

     Emails.sort(key=lambda s: s.partition("@")[::-1])

also see:

     http://preview.tinyurl.com/yc3qak
     http://effbot.org/zone/python-list.htm#sorting

</F>




More information about the Python-list mailing list