Sorting email addresses by domain

hanzhupeng hanzhupeng at kingsoft.net
Wed Nov 10 21:49:21 EST 2004


Jp Calderone:

	 It's cool . tkx
	



>On Thu, 11 Nov 2004 01:58:35 -0000, "Peter Murray" <pdmurray at ntlworld.com> wrote:
>>Hi! I hope someone can assist me, I have been using Python for a year or
>> two, but I am not a programmer and only use it occasionally for small issues
>> or just for fun. So I perhaps do not have the right 'mindset' to always see
>> the answer to a problem.
>> 
>> Anyway, I have a list of email address and I want to sort them by domain.
>> List.sort() wont work as this looks at the start of each address only. So
>> the answer I have come up with is to split each email address around the
>> "@", swap the position of the name and domain, build a new list with these
>> and then sort this. Then switch positions back, and join with an "@". It
>> works, but it seems a little inelegant. Is there a better, or easier way to
>> do this?
>
>  In Python 2.3,
>
>    def key(addr):
>        parts = addr.split('@')
>        parts.reverse()
>        return parts
>    addresses.sort(lambda a, b: cmp(key(a), key(b)))
>
>  In Python 2.4, re-using the above definition of key:
>
>    addresses.sort(key=key)
>
>  Jp
>-- 
>http://mail.python.org/mailman/listinfo/python-list

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        hanzhupeng
        hanzhupeng at kingsoft.net
          2004-11-11



More information about the Python-list mailing list