So I have a list of string paths:
x = ['../../scene/temp_5a/458754/1_car.png',
'../../scene/temp_5a/458754/2_car.png',
'../../scene/temp_5a/458754/10_car.png',
'../../scene/temp_5a/458754/15_car.png',
'../../scene/temp_5a/458754/3_car.png']
And I need to sort it by the number in front of _car
. Does anyone know of a quick way to do this?
I currently have this but it seems like the split is getting all the digits. I only want to get the digit in front in front of _car
.
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
return [ atoi(c) for c in re.split('(\d+)', text) ]
x.sort(key=natural_keys) # gives an error