Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sorting a list numbers stored as strings

Reply
Thread Tools

sorting a list numbers stored as strings

 
 
aine_canby@yahoo.com
Guest
Posts: n/a
 
      09-24-2007
hi,

I have the following list -

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

how do I sort it like this -

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

thanks,

aine

 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-24-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Amit Khemka
Guest
Posts: n/a
 
      09-24-2007
On 9/24/07, (E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
Carsten Haese
Guest
Posts: n/a
 
      09-24-2007
On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
> On 9/24/07, (E-Mail Removed) <(E-Mail Removed)> 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


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      09-24-2007
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
 
Reply With Quote
 
Michael J. Fromberger
Guest
Posts: n/a
 
      09-24-2007
In article <(E-Mail Removed) .com>,
(E-Mail Removed) 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
 
Reply With Quote
 
Amit Khemka
Guest
Posts: n/a
 
      09-25-2007
On 9/24/07, Carsten Haese <(E-Mail Removed)> wrote:
> On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
> > On 9/24/07, (E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting numbers as strings Jack Bauer Ruby 13 05-20-2009 06:04 AM
RE: sorting a list numbers stored as strings Delaney, Timothy (Tim) Python 4 09-25-2007 06:36 PM
Numbers to strings to numbers again one man army Javascript 6 12-30-2005 07:05 AM
Sorting of numbers or strings. Mars C Programming 3 03-02-2005 06:13 PM
Sorting strings with characters and numbers Carsten Zerbst Java 6 08-14-2003 08:36 PM



Advertisments