![]() |
Sorting integer lists
Im studying python and I am unsure of how to solve a problem.
Im taking user input of two strings consisting of integers. I split these on whitespace into two lists, merge the lists, and sort it. My problem is that these lists are being sorted by character and not integers (i.e. 22 > 3). Ive searched for a solution to this but have yet to find one. Does Python know these are characters? Can I convert each value, element-by-element into integers and expect it to sort them numerically? Thanks. |
:
>>> int("23") |
Some additional useful code:
:
|
Thanks DaWei but I was already aware that the issue was with they type of data in the list. What I was looking for was an approach like Arevos posted. I resorted to converting each value one by one to an int, but I was looking for a built-in way. It seems "map()" is what I was looking for. I'll read more about it.
|
Re: Sorting integer lists
I realize this thread is rather old, but for reference, you can also use your list's sort() method if you add a parameter to it.
:
numList = ["12","4","66","55"] |
| All times are GMT -5. The time now is 3:09 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC