hi,
I have the following list -
["1", "11", "2", "22"]
how do I sort it like this -
["1", "2", "11", "22"]
thanks,
aine 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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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))
...
|
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)...
|
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:
...
|
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...
|
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
|
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....
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |