Jan-22-2021, 05:15 PM
Here's one option :
from itertools import groupby def keyfunction (string) : return [int (''.join (group)) if key else ''.join (group) for key, group in groupby (string, str.isdigit)] test_list = ['A35','A4', 'B3', 'A1', 'A15'] print (sorted (test_list, key = keyfunction))If you can and want to install
natsort
with pip, here's another option :import natsort test_list = ['A35','A4', 'B3', 'A1', 'A15'] print (natsort.natsorted (test_list))