sorting a list numbers stored as strings

Discussion in 'Python' started by aine_canby@yahoo.com, Sep 24, 2007.

  1. Guest

    hi,

    I have the following list -

    ["1", "11", "2", "22"]

    how do I sort it like this -

    ["1", "2", "11", "22"]

    thanks,

    aine
    , Sep 24, 2007
    #1
    1. Advertising

  2. a écrit :
    > hi,
    >
    > I have the following list -
    >
    > ["1", "11", "2", "22"]
    >
    > how do I sort it like this -
    >
    > ["1", "2", "11", "22"]


    source = ["1", "11", "2", "22"]
    result = [t[1] for t in sorted((int(item), item) for item in source)]
    print result
    Bruno Desthuilliers, Sep 24, 2007
    #2
    1. Advertising

  3. Amit Khemka Guest

    On 9/24/07, <> wrote:
    > hi,
    >
    > I have the following list -
    >
    > ["1", "11", "2", "22"]
    >
    > how do I sort it like this -
    >
    > ["1", "2", "11", "22"]
    >


    Hi,

    >>> l = ["1", "11", "2", "22"]
    >>> sorted(l, cmp = lambda x, y: cmp(int(x), int(y))) # provide your

    own compare function !
    >>> l

    ['1', '2', '11', '22']

    Cheers,

    --
    ----
    Amit Khemka
    website: www.onyomo.com
    wap-site: www.owap.in
    Amit Khemka, Sep 24, 2007
    #3
  4. On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
    > On 9/24/07, <> wrote:
    > > hi,
    > >
    > > I have the following list -
    > >
    > > ["1", "11", "2", "22"]
    > >
    > > how do I sort it like this -
    > >
    > > ["1", "2", "11", "22"]
    > >

    >
    > Hi,
    >
    > >>> l = ["1", "11", "2", "22"]
    > >>> sorted(l, cmp = lambda x, y: cmp(int(x), int(y))) # provide your

    > own compare function !
    > >>> l

    > ['1', '2', '11', '22']


    That interpreter session is a work of fiction, since sorted returns the
    sorted list instead of sorting the list in place. Also, it's better
    (i.e. more readable and likely faster) to use a sort key function
    instead of a comparison function whenever possible. In this case, the
    sort key function is particularly trivial:

    >>> l = ["1", "11", "2", "22"]
    >>> sorted(l, key=int)

    ['1', '2', '11', '22']

    --
    Carsten Haese
    http://informixdb.sourceforge.net
    Carsten Haese, Sep 24, 2007
    #4
  5. Peter Otten Guest

    aine_canby wrote:

    > I have the following list -
    >
    > ["1", "11", "2", "22"]
    >
    > how do I sort it like this -
    >
    > ["1", "2", "11", "22"]


    >>> items = ["1", "11", "2", "22"]
    >>> items.sort(key=int)
    >>> items

    ['1', '2', '11', '22']

    This is more efficient than Amit's compare function and even Bruno's
    decorate-sort-undecorate (DSU) -- which of course only matters if the list
    becomes a bit larger.

    Peter
    Peter Otten, Sep 24, 2007
    #5
  6. In article <>,
    wrote:

    > hi,
    >
    > I have the following list -
    >
    > ["1", "11", "2", "22"]
    >
    > how do I sort it like this -
    >
    > ["1", "2", "11", "22"]
    >
    > thanks,
    >
    > aine


    Try:

    lst.sort(key = lambda s: int(s))

    Assuming, of course, that "lst" is your original list; this will sort it
    in place. Also, you should probably read about the "sorted" function
    here:

    <http://docs.python.org/lib/built-in-funcs.html>

    Cheers,
    -M

    --
    Michael J. Fromberger | Lecturer, Dept. of Computer Science
    http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
    Michael J. Fromberger, Sep 24, 2007
    #6
  7. Amit Khemka Guest

    On 9/24/07, Carsten Haese <> wrote:
    > On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
    > > On 9/24/07, <> wrote:

    <snip>
    > >
    > > >>> l = ["1", "11", "2", "22"]
    > > >>> sorted(l, cmp = lambda x, y: cmp(int(x), int(y))) # provide your

    > > own compare function !
    > > >>> l

    > > ['1', '2', '11', '22']

    >
    > That interpreter session is a work of fiction, since sorted returns the
    > sorted list instead of sorting the list in place.


    I am sorry, thanks for pointing out !
    What I intended to write was:

    >>> l = sorted(l, cmp = lambda x, y: cmp(int(x), int(y)))


    Btw, It was more of a goofed up Reality show !

    cheers,
    --
    ----
    Amit Khemka
    website: www.onyomo.com
    wap-site: www.owap.in
    Amit Khemka, Sep 25, 2007
    #7
    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. Carsten Zerbst
    Replies:
    6
    Views:
    6,785
    Roedy Green
    Aug 14, 2003
  2. Mars

    Sorting of numbers or strings.

    Mars, Mar 2, 2005, in forum: C Programming
    Replies:
    3
    Views:
    329
    Eric Sosman
    Mar 2, 2005
  3. Delaney, Timothy (Tim)

    RE: sorting a list numbers stored as strings

    Delaney, Timothy (Tim), Sep 25, 2007, in forum: Python
    Replies:
    4
    Views:
    405
  4. Jack Bauer

    Sorting numbers as strings

    Jack Bauer, May 18, 2009, in forum: Ruby
    Replies:
    13
    Views:
    238
    Johan Holmberg
    May 20, 2009
  5. one man army

    Numbers to strings to numbers again

    one man army, Dec 28, 2005, in forum: Javascript
    Replies:
    6
    Views:
    132
    one man army
    Dec 30, 2005
Loading...

Share This Page