converting a set into a sorted list

M

MackS

Dear all,

I've got several large sets in my program. After performing several
operations on these I wish to present one set to the user [as a list]
sorted according to a certain criterion. Is there any direct way to do
so? Or must I

list = []

for item in set1:
list.append(item)

list.sort(....)

Can I somehow avoid doing this in two stages? Can I somehow avoid first
creating a long list only to immediately sort it afterwards?

Thanks for any guidance,

Mack
 
R

Robert Kern

MackS said:
Dear all,

I've got several large sets in my program. After performing several
operations on these I wish to present one set to the user [as a list]
sorted according to a certain criterion. Is there any direct way to do
so? Or must I

list = []

for item in set1:
list.append(item)

list.sort(....)

Can I somehow avoid doing this in two stages? Can I somehow avoid first
creating a long list only to immediately sort it afterwards?

In Python 2.4,

In [1]:sorted?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function sorted>
Namespace: Python builtin
Docstring:
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
B

Brian Beck

Robert said:
MackS said:
Dear all,

I've got several large sets in my program. After performing several
operations on these I wish to present one set to the user [as a list]
sorted according to a certain criterion. Is there any direct way to do
so? Or must I

list = []

for item in set1:
list.append(item)

list.sort(....)

So, for this example, just do:

sorted(set1)
 
T

Terry Reedy

MackS said:
Can I somehow avoid doing this in two stages? Can I somehow avoid first
creating a long list only to immediately sort it afterwards?

Yes, you could interatively extract and append the min of the set
(selection sort), but this would be O(n**2), like bubble or insert sort,
instead of the O(n*logn) of make list and sort. You can do the latter in
two statements without append:
display = list(big_set)
display.sort().

As Robert noted, this can be condensed to one using sorted, but that will
do the same as the two lines above.

Terry J. Reedy
 
B

Bengt Richter

MackS said:
Dear all,

I've got several large sets in my program. After performing several
operations on these I wish to present one set to the user [as a list]
sorted according to a certain criterion. Is there any direct way to do
so? Or must I

list = []

for item in set1:
list.append(item)

list.sort(....)

Can I somehow avoid doing this in two stages? Can I somehow avoid first
creating a long list only to immediately sort it afterwards?

In Python 2.4,

In [1]:sorted?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function sorted>
Namespace: Python builtin
Docstring:
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
That's plenty of information, but IMO "key=None" doesn't hint strongly enough
about what you can do with it, so I'd advise reading about all the parameters ;-)

Regards,
Bengt Richter
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top