473,326 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to sort list

Lad
I have a list of emails and I would like to sorted that list by domains
E.g.
If the list is

Emails=['a@hotmail.com','a@yahoo.com','b@hotmail.com','c@y ahoo.com',....]

after sorting I would like to have
Emails=['a@hotmail.com','b@hotmail.com','a@yahoo.com','c@y ahoo.com',....]

What is the best/easiest way?

Thank you for help
L.

Nov 21 '06 #1
7 8952
Lad <py****@hope.czwrote:
I have a list of emails and I would like to sorted that list by
domains
E.g.
If the list is

Emails=['a@hotmail.com','a@yahoo.com','b@hotmail.com','c@y ahoo.com',....]

after sorting I would like to have
Emails=['a@hotmail.com','b@hotmail.com','a@yahoo.com','c@y ahoo.com',....]

What is the best/easiest way?
Decorate-sort-undecorate?

#v+

array = []

for addr in Emails:
(user, domain) = addr.split('@')
array.append((domain, user, addr))
# end for

array.sort()

SortedEmails = [addr for (user, domain, addr) in array]

#v-

Cheers,

--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/
Nov 21 '06 #2
Emails=['a@hotmail.com','a@yahoo.com','b@hotmail.com','c@y ahoo.com',....]
after sorting I would like to have
Emails=['a@hotmail.com','b@hotmail.com','a@yahoo.com','c@y ahoo.com',....]
This problem isn't easy to solve in the general way, this is a first
try:

emails = ['a@hotmail.com','a@yahoo.com','b@hotmail.com','c@y ahoo.com']

print sorted(emails, key=lambda em: em.split("@")[::-1])

Bye,
bearophile

Nov 21 '06 #3


On Nov 21, 8:15 pm, "Lad" <pyt...@hope.czwrote:
I have a list of emails and I would like to sorted that list by domains
E.g.
If the list is

Emails=['...@hotmail.com','...@yahoo.com','...@hotmail.com ','...@yahoo.com',....]

after sorting I would like to have
Emails=['...@hotmail.com','...@hotmail.com','...@yahoo.com ','...@yahoo.com',....]

What is the best/easiest way?

Thank you for help
L.
Another way would be a compare function as callback function in the
sort() method of list:

py>def compare(a, b):
py a = a.split('@')[1]
py b = b.split('@')[1]
py return cmp(a, b)
py>
py>Emails =
['...@hotmail.com','...@yahoo.com','...@hotmail.com ','...@yahoo.com']
py>Emails.sort(compare)
py>print Emails
['...@hotmail.com', '...@hotmail.com', '...@yahoo.com', '...@yahoo.com']

Nov 21 '06 #4

Lad wrote:
I have a list of emails and I would like to sorted that list by domains
E.g.
If the list is

Emails=['a@hotmail.com','a@yahoo.com','b@hotmail.com','c@y ahoo.com',....]

after sorting I would like to have
Emails=['a@hotmail.com','b@hotmail.com','a@yahoo.com','c@y ahoo.com',....]

What is the best/easiest way?

Thank you for help
L.
What have you tried?
What issues did you have with your attempts?

- Paddy.

Nov 21 '06 #5
Klaus Alexander Seistrup wrote:
Decorate-sort-undecorate?

#v+

array = []

for addr in Emails:
(user, domain) = addr.split('@')
array.append((domain, user, addr))
# end for

array.sort()

SortedEmails = [addr for (user, domain, addr) in array]

#v-
note that DSU is built into Python these days:

L.sort(key=transform)

so you could use e.g.

Emails.sort(key=lambda s: s.partition("@")[::-1])

also see:

http://preview.tinyurl.com/yc3qak
http://effbot.org/zone/python-list.htm#sorting

</F>

Nov 22 '06 #6
Fredrik Lundh wrote:
note that DSU is built into Python these days:

L.sort(key=transform)
Sweet, thanks for the hint.

Cheers,

--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/
Nov 22 '06 #7
In article <11**********************@m73g2000cwd.googlegroups .com>,
"Lad" <py****@hope.czwrote:
I have a list of emails and I would like to sorted that list by domains
E.g.
If the list is

Emails=['a@hotmail.com','a@yahoo.com','b@hotmail.com','c@y ahoo.com',....]

after sorting I would like to have
Emails=['a@hotmail.com','b@hotmail.com','a@yahoo.com','c@y ahoo.com',....]

What is the best/easiest way?
One reasonable option is to use the .sort() method of a list:

Emails.sort(key = lambda s: list(reversed(s.split('@'))))

The "key" parameter specifies how to obtain a sort key from each element
in the source list.

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Nov 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Pinard | last post by:
(Got a comm error trying to post first time, sorry if this is a duplicate) New to Python, so please bear with me. >>> import sys >>> print sys.modules.keys() # works fine >>>...
3
by: Brian McGonigle | last post by:
I'm a Perl programmer learning Python (up to chapter 7 in Learning Python, so go easy on me :-) and I find that I look to do things in Python the way I would do them in Perl. In Perl functions and...
12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
11
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
99
by: Shi Mu | last post by:
Got confused by the following code: >>> a >>> b >>> c {1: , ], 2: ]} >>> c.append(b.sort()) >>> c {1: , ], 2: , None]}
0
by: Amar | last post by:
Hi, I have a generic list with a some objects of a particular class in it. I have implemented a IComparer for the the class and pass it to the List. The list.sort method works fine when the value...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.