Sorting email addresses by domain

P

Peter Murray

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
 
G

Greg Krohn

Peter Murray wrote:
[snip]
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?
[snip code]

Elegant or Perlish, you decide:

addresses.sort(lambda a, b: cmp(a[a.find('@'):].lower(),
b[b.find('@'):].lower()))

Note that this isn't case sensitive and items without an '@' will be
placed at the end.

greg
 
J

James Davies

Split the email addresses on the '@', and store the domain in the key of a
dictionary, then sort the keys.

i.e.
data = {}
for email in emails:
(user, domain) = email.split('@')
data[domain] = email
keys = data.keys()
keys.sort()
return [data[x] for x in keys]


still not very elegant (would be nicer if list.sort() returned rather than
mutated the list).

Typed from memory, not checked. Possibly major errors in it :p
 
E

EP

From: Greg Krohn @ invalid said:
Elegant or Perlish, you decide:

addresses.sort(lambda a, b: cmp(a[a.find('@'):].lower(),
b[b.find('@'):].lower()))

Note that this isn't case sensitive and items without an '@' will be
placed at the end.


I think: yes and yes. Looks efficient versus what came to mind for me:
domains=[a.split("@")[1] for a in addies]
aPairs=zip(domains,addies)
aPairs.sort()
sortedAddies=[tup[1] for tup in aPairs]
return sortedAddies


I do like having functions with obvious names so that I don't have to trace the logic to remember what they do...
 
M

Maciej Dziardziel

Peter said:
Anyway, I have a list of email address and I want to sort them by domain.

def cmpdomain(email1,email2):
[usr1,dmn1],[usr2,dmn2] = email1.split('@'),email2.split('@')
if email1 == email2: return 0
if dmn1 == dmn2: return cmp(usr1,usr2)
return cmp(dmn1,dmn2)



emails.sort(cmpdomain)
 
M

Mike Meyer

EP said:
From: Greg Krohn @ invalid said:
Elegant or Perlish, you decide:

addresses.sort(lambda a, b: cmp(a[a.find('@'):].lower(),
b[b.find('@'):].lower()))

Note that this isn't case sensitive and items without an '@' will be
placed at the end.


I think: yes and yes. Looks efficient versus what came to mind for me:
def sortByDomain(addies=[]):
domains=[a.split("@")[1] for a in addies]
aPairs=zip(domains,addies)
aPairs.sort()
sortedAddies=[tup[1] for tup in aPairs]
return sortedAddies

I wouldn't bet on the one using an internal cmp being faster than
doing the decorate-sort-undecorate version. In fact, my tests on a
list of 16000 addresses show them to be virtually identical. Tuning
your version to get rid of one trip through the list doesn't make much
difference:

def sort_by_domain(addies):
l = [(a.split("@")[1], a) for a in addies]
l.sort()
return [a[1] for a in l]

<mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top