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
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