Reputation: 99
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']
Upvotes: 0
Views: 111
Reputation: 116
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.
Upvotes: 0
Reputation: 1438
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.
Upvotes: 1
Reputation: 22954
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']
Upvotes: 1