FAQ
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

Search Discussions

  • Robert Kern at May 15, 2005 at 2:20 am

    MackS wrote:
    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
    rkern at ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
  • Brian Beck at May 15, 2005 at 3:06 am

    Robert Kern wrote:
    MackS wrote:
    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)


    --
    Brian Beck
    Adventurer of the First Order
  • Terry Reedy at May 15, 2005 at 3:07 am
    "MackS" <mackstevenson at hotmail.com> wrote in message
    news:1116120701.520116.315230 at f14g2000cwb.googlegroups.com...
    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
  • MackS at May 15, 2005 at 6:05 am
    Thank you for the pointer. I'll upgrade to 2.4.

    Best,

    Mack
  • Bengt Richter at May 15, 2005 at 7:42 am

    On Sat, 14 May 2005 19:20:24 -0700, Robert Kern wrote:
    MackS wrote:
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 15, '05 at 1:31a
activeMay 15, '05 at 7:42a
posts6
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase