Re: totally lost newbie

Discussion in 'Python' started by Steve Howell, May 27, 2007.

  1. Steve Howell

    Steve Howell Guest

    --- Marc 'BlackJack' Rintsch <> wrote:
    >
    > Maybe it's easier to use a key function instead of a
    > compare function. A
    > key function receives an element and must return
    > something that is then
    > sorted and the element ends up where the computed
    > key is in the sorted
    > list. Little example for sorting a list of strings
    > first by length and
    > strings of the same length by alphabetical order:
    >
    > def key_func(item):
    > return (len(item), item)
    >
    > data = ['viking', 'spam', 'parrot', 'ham', 'eric']
    > data.sort(key=key_func)
    > print data
    >


    Marc, when did the key feature get introduced, 2.4 or
    2.5? I'm asking on behalf of the newbie, who's going
    to struggle with your solution if he's still running
    2.3.




    ____________________________________________________________________________________Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
     
    Steve Howell, May 27, 2007
    #1
    1. Advertising

  2. In <>, Steve Howell
    wrote:

    >> def key_func(item):
    >> return (len(item), item)
    >>
    >> data = ['viking', 'spam', 'parrot', 'ham', 'eric']
    >> data.sort(key=key_func)
    >> print data
    >>

    >
    > Marc, when did the key feature get introduced, 2.4 or
    > 2.5? I'm asking on behalf of the newbie, who's going
    > to struggle with your solution if he's still running
    > 2.3.


    It's available in 2.4 but I don't know when it was introduced.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, May 27, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    10
    Views:
    559
    Steve
    Jan 13, 2004
  2. karthik kumar

    stl errors - totally lost

    karthik kumar, Dec 3, 2004, in forum: C++
    Replies:
    8
    Views:
    681
    Karl Heinz Buchegger
    Dec 3, 2004
  3. guardsman85
    Replies:
    2
    Views:
    561
    guardsman85
    Aug 29, 2006
  4. mark

    totally lost newbie

    mark, May 27, 2007, in forum: Python
    Replies:
    3
    Views:
    534
    Kay Schluehr
    May 27, 2007
  5. Steve Howell

    Re: totally lost newbie

    Steve Howell, May 27, 2007, in forum: Python
    Replies:
    0
    Views:
    480
    Steve Howell
    May 27, 2007
Loading...

Share This Page