Sorting email addresses by domain

From: Peter Murray (pdmurray_at_ntlworld.com)
Date: 11/11/04

  • Next message: John Machin: "Re: Announce: csv_xs.py"
    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


  • Next message: John Machin: "Re: Announce: csv_xs.py"

    Relevant Pages

    • Re: Age thread.
      ... in common which alienate the mortal programmer, then it just is like that - ... programming psychology, designed his language to match, and it prospers. ... Now the en-vogue languages are Python and Ruby; ... This sort of language comes ...
      (comp.lang.forth)
    • Re: Intermediate to expert book
      ... > I'm an experienced C programmer and I learnt Python from the "Learning ... > Python" O'Reilly book because it had good reviews. ... > sort of style I'm after. ... > or too deeply buried in Learning Python. ...
      (comp.lang.python)
    • Re: Simulation Programming Skills and Python
      ... and a strong programmer in any language should be able to ... whether all programmers are capable of that is sort of like asking ... As for Python, it's a good language for prototyping. ... compiled languages. ...
      (comp.lang.python)
    • Re: "Sorting" assignment
      ... is the fastest possible way of exchanging two ... implement the exchange in a sort. ... In your Employee example, you never have to copy actual class ... programmer reasons that if his code is doin' a lotta data movin' (like ...
      (comp.programming)
    • Re: Pythons simplicity philosophy
      ... > Python implementation might like to implement the ... That's not a problem with Jython, since Java has a stable sort, see ... There was a OCaml implemenation of a Python variant, ...
      (comp.lang.python)