How to sort a list of strings numerically?

I know this sounds trivial, but I did not understand that the sort() function of Python was weird. I have a list of "numbers" that are actually in string form, so I convert them to ints first and then try to sort.

 list1=["1","10","3","22","23","4","2","200"] for item in list1: item=int(item) list1.sort() print list1 

Gives me:

 ['1', '10', '2', '200', '22', '23', '3', '4'] 

I want to

 ['1','2','3','4','10','22','23','200'] 

I looked at some algorithms related to sorting numeric sets, but the ones I found include sorting alphanumeric sets.

I know this is probably not a problem, but google and my tutorial do not offer anything more or less useful than the .sort() function.

+100
python sorting
Aug 6 '10 at 17:17
source share
13 answers

You have not actually converted your strings to integers. Rather, you did, but then did nothing with the results. What you want is:

 list1 = ["1","10","3","22","23","4","2","200"] list1 = [int(x) for x in list1] list1.sort() 

If for some reason you need to save strings instead of integers (usually this is a bad idea, but maybe you need to keep leading zeros or something like that), you can use a key function. sort takes a named parameter key , which is a function that is called for each element before comparing it. The return values โ€‹โ€‹of the key function are compared instead of directly comparing list items:

 list1 = ["1","10","3","22","23","4","2","200"] # call int(x) on each element before comparing it list1.sort(key=int) 
+169
Aug 6 2018-10-06T00:
source share

You can pass the function to the key parameter in the .sort method . In this case, the system will sort by key (x) instead of x.

 list1.sort(key=int) 



BTW, to convert the list to integers forever, use the map function

 list1 = list(map(int, list1)) # you don't need to call list() in Python 2.x 

or list comprehension

 list1 = [int(x) for x in list1] 
+35
Aug 06 '10 at 17:18
source share

If you want to use the sorted() function: sorted(list1, key=int)

It returns a new sorted list.

+16
Dec 28 '12 at 18:28
source share

The Python variety is not strange. It is just that this code:

 for item in list1: item=int(item) 

does not do what you think - item does not return back to the list, it is simply discarded.

In any case, the correct solution is to use key=int , as others have shown you.

+12
Aug 6 2018-10-06T00:
source share

You can also use:

 import re def sort_human(l): convert = lambda text: float(text) if text.isdigit() else text alphanum = lambda key: [ convert(c) for c in re.split('([-+]?[0-9]*\.?[0-9]*)', key) ] l.sort( key=alphanum ) return l
import re def sort_human(l): convert = lambda text: float(text) if text.isdigit() else text alphanum = lambda key: [ convert(c) for c in re.split('([-+]?[0-9]*\.?[0-9]*)', key) ] l.sort( key=alphanum ) return l 

this is very similar to other things you can find on the Internet, but also works for alphanumeric characters like [abc0.1, abc0.2 ..]

+8
Oct 14 '16 at 9:23
source share

Seamus Campbell's answer does not work on python2.x.
list1 = sorted(list1, key=lambda e: int(e)) using the lambda function works well.

+6
Apr 22 '17 at 7:41
source share

Try this, it will sort the list in place in descending order (in this case there is no need to specify a key):

Process

 listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9] listC = sorted(listB, reverse=True) # listB remains untouched print listC 

exit:

  [48, 46, 25, 24, 22, 13, 8, -9, -15, -36] 
+3
Oct 13 '18 at 8:55
source share

The most recent solution is correct. You read the solutions as a string, in which case the order is 1, then 100, then 104, then 2, then 21, then 2001001010, 3, etc.

Instead, you should WRITE your input as an int:

sorted rows:

stringList = (1, 10, 2, 21, 3)

sorted ints:

intList = (1, 2, 3, 10, 21)

To distinguish, just put a stringList inside an int (blahblah).

Again:

 stringList = (1, 10, 2, 21, 3) newList = int (stringList) print newList => returns (1, 2, 3, 10, 21) 
+1
Dec 06 '13 at 0:15
source share

Yesterday, I approached the same problem and found a natsort module that solves problems. Using:

 from natsort import natsorted # Example list of strings a = ['1', '10', '2', '3', '11'] [In] sorted(a) [Out] ['1', '10', '11', '2', '3'] [In] natsorted(a) [Out] ['1', '2', '3', '10', '11'] 
+1
May 23 '18 at 18:00
source share

An easy way to sort a number list

  numlists = [5,50,7,51,87,97,53] numlists.sort(reverse=False) print(numlists) 
+1
Sep 04 '18 at 5:10
source share

if you want to use a string of numbers, better take a different list, as shown in my code, it will work fine.

 list1=["1","10","3","22","23","4","2","200"] k=[] for item in list1: k.append(int(item)) k.sort() print(k) 
0
Sep 28 '18 at 10:52
source share

Python3 Sort Function:

 if __name__== '__main__': n = list(input()) print(n) print(sorted(n)) 
0
Mar 01 '19 at 13:03
source share
 scores = ['91','89','87','86','85'] scores.sort() print (scores) 

This worked for me using python 3 version, although it was not in version 2.

-one
Jul 19 '16 at 10:56
source share



All Articles