473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

sorting a list numbers stored as strings

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
6 1601
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: D. Roshani | last post by:
Hello ! I wonder if any one can help me to create a cosomize sorting order (as Macro or added small program in c++ or c# which does this work) in a Access Database contaning one table only words...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort†method. For example: ...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
3
by: melanieab | last post by:
Hi, When you click on a column header to sort in ascending or descending order, numbers (and dates) aren't sorted correctly. It turns up like this: 1 10 11 2 3 4
9
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on....
1
by: Mansi Shah | last post by:
Hi All, I have used gridview in my application and i m using dataview to sort the grid. But when i sort the numeric column, dataview sorts it like a string. So, after sorting, instead of...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.