Thursday 12 September 2013

Python double sort with split string

Python double sort with split string

I am trying to double sort something and it seems to be forgetting the
first sort, I thought python uses stable sort so I am probably making a
mistake.
Original text looks like this:
ABC - 0.2 - 15
BAC - 1.2 - 10
ABC - 1.3 - 29
ABC - 0.7 - 11
I want:
ABC - 0.2 - 15
ABC - 0.7 - 11
ABC - 1.3 - 29
BAC - 1.2 - 10
Here is my code:
def akey(a):
z = a.split(' -')
v = [z[0]]
x = [str(i) for i in v]
return x
def bkey(b):
z = b.split(' -')
v = [z[1]]
x = [float(i) for i in v]
return x
labelList.sort(key=akey)
labelList.sort(key=bkey)
Thanks for the help

No comments:

Post a Comment