Sorting email addresses by domain
Mike Meyer
mwm at mired.org
Thu Nov 11 15:36:04 EST 2004
"EP" <EP at zomething.com> writes:
> From: Greg Krohn @ invalid wrote:
>
>> 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
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list