0

I am a beginner in python. i sorted a list of string values but '8,13' '8,14'.... and '9,11' '9,12'.... values came to last. what causes this error? I typed this in terminal.

a = ['13,7', '14,7', '15,7', '12,8', '13,8', '14,8', '15,8', '16,8', 
     '11,9', '12,9', '13,9', '16,9', '10,10', '11,10', '12,10', 
     '16,10', '9,11', '10,11', '11,11', '16,11', '9,12', '10,12', 
     '16,12', '8,13', '9,13', '10,13', '15,13', '16,13', '8,14', 
     '9,14', '15,14', '16,14', '17,14', '8,15', '9,15', '14,15', 
     '16,15', '17,15', '18,15', '19,15', '8,16', '9,16', '12,16', 
     '13,16', '17,16', '18,16', '19,16', '8,17', '9,17', '10,17', 
     '11,17', '12,17', '9,18', '10,18', '11,18']

a.sort()

a
#['10,10', '10,11', '10,12', '10,13', '10,17', '10,18', '11,10', 
# '11,11', '11,17', '11,18', '11,9', '12,10', '12,16', '12,17', 
# '12,8', '12,9', '13,16', '13,7', '13,8', '13,9', '14,15', '14,7', 
# '14,8', '15,13', '15,14', '15,7', '15,8', '16,10', '16,11', '16,12', 
# '16,13', '16,14', '16,15', '16,8', '16,9', '17,14', '17,15', 
# '17,16', '18,15', '18,16', '19,15', '19,16', '8,13', '8,14', '8,15', 
# '8,16', '8,17', '9,11', '9,12', '9,13', '9,14', '9,15', '9,16', 
# '9,17', '9,18']
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
rajini raja
  • 99
  • 3
  • 13

3 Answers3

1

Your list elements are of type str, so you need to either convert them to int tuple before sorting or you may use lambda as key argument to sort method for on the fly conversion as:

>>> a.sort(key=lambda x:map(int, x.split(",")))
>>> ['8,13', '8,14', '8,15', '8,16', '8,17', '9,11', '9,12', '9,13', '9,14', '9,15', '9,16', '9,17', '9,18', '10,10', '10,11', '10,12', '10,13', '10,17', '10,18', '11,9', '11,10', '11,11', '11,17', '11,18', '12,8', '12,9', '12,10', '12,16', '12,17', '13,7', '13,8', '13,9', '13,16', '14,7', '14,8', '14,15', '15,7', '15,8', '15,13', '15,14', '16,8', '16,9', '16,10', '16,11', '16,12', '16,13', '16,14', '16,15', '17,14', '17,15', '17,16', '18,15', '18,16', '19,15', '19,16']
ZdaR
  • 22,343
  • 7
  • 66
  • 87
1

This should do it. The commas require an additional step.

a = [float(i.replace(',', '.')) for i in a]
a.sort()

Reference this post: How can I convert a string with dot and comma into a float number in Python for the actual Pythonic way to handle numbers that use commas instead of decimal points.

n8sty
  • 1,418
  • 1
  • 14
  • 26
  • @rajiniraja Doesn't this destroy your data? You said the values represent two ints, and after this you don't have them anymore. – Stefan Pochmann Aug 26 '17 at 06:51
  • Indeed it destroy my data, but I get the idea. I split the string by comma as separator and convert it into int and append it to list. Finally sort function results my desired output. Thanks for the efforts. – rajini raja Aug 26 '17 at 12:07
0

I guess what you are looking for is natural sort. Use natsort a third party library and sort.

>>> import natsort
>>> natsort.natsorted(a)
['8,13', '8,14', '8,15', '8,16', '8,17', '9,11', '9,12', '9,13', '9,14', '9,15', '9,16', '9,17', '9,18', '10,10', '10,11', '10,12', '10,13', '10,17', '10,18', '11,9', '11,10', '11,11', '11,17', '11,18', '12,8', '12,9', '12,10', '12,16', '12,17', '13,7', '13,8', '13,9', '13,16', '14,7', '14,8', '14,15', '15,7', '15,8', '15,13', '15,14', '16,8', '16,9', '16,10', '16,11', '16,12', '16,13', '16,14', '16,15', '17,14', '17,15', '17,16', '18,15', '18,16', '19,15', '19,16']

Sorting is between strings. It compares letter by letter of the string. eg when comparing string '10,10' and '9,18' its first compares '1' and '9' then '0' and ',' so on. That's the reason you are not getting the desired result.

Sabu George
  • 116
  • 1
  • 3