Sorting email addresses by domain
From: Peter Murray (pdmurray_at_ntlworld.com)
Date: 11/11/04
- Previous message: Anthony Papillion II: "mod_python not working (showing source code)"
- Next in thread: Jp Calderone: "Re: Sorting email addresses by domain"
- Maybe reply: Jp Calderone: "Re: Sorting email addresses by domain"
- Reply: Greg Krohn: "Re: Sorting email addresses by domain"
- Reply: James Davies: "Re: Sorting email addresses by domain"
- Reply: Maciej Dziardziel: "Re: Sorting email addresses by domain"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: <python-list@python.org> Date: Thu, 11 Nov 2004 01:58:35 -0000
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?
Code is below. Thanks for any comments!
Pete.
def sortlist(addresslist):
l1 = []
for i in addresslist:
temp = i.split("@")
temp[0], temp[1] = temp[1], temp[0]
l1.append(temp)
l1.sort()
l2 = []
for i in l1:
i[0], i[1] = i[1], i[0]
temp = "@".join(i)
l2.append(temp)
return l2
- Previous message: Anthony Papillion II: "mod_python not working (showing source code)"
- Next in thread: Jp Calderone: "Re: Sorting email addresses by domain"
- Maybe reply: Jp Calderone: "Re: Sorting email addresses by domain"
- Reply: Greg Krohn: "Re: Sorting email addresses by domain"
- Reply: James Davies: "Re: Sorting email addresses by domain"
- Reply: Maciej Dziardziel: "Re: Sorting email addresses by domain"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|