424,066 Members | 2,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,066 IT Pros & Developers. It's quick & easy.

sorting a list numbers stored as strings

P: n/a
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 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ai********@yahoo.com 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

Sep 24 '07 #2

P: n/a
On 9/24/07, ai********@yahoo.com <ai********@yahoo.comwrote:
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
Sep 24 '07 #3

P: n/a
On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
On 9/24/07, ai********@yahoo.com <ai********@yahoo.comwrote:
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
Sep 24 '07 #4

P: n/a
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
Sep 24 '07 #5

P: n/a
In article <11**********************@y42g2000hsy.googlegroups .com>,
ai********@yahoo.com 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
Sep 24 '07 #6

P: n/a
On 9/24/07, Carsten Haese <ca*****@uniqsys.comwrote:
On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
On 9/24/07, ai********@yahoo.com <ai********@yahoo.comwrote:
<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
Sep 25 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.