慕姐4208626
这种方式使用groupby,并适用于字母和数字之间的任意数量的交换from itertools import groupbydef keyfunc(s): return [int(''.join(g)) if k else ''.join(g) for k, g in groupby(s, str.isdigit)]sorted(my_list, key=keyfunc)演示:>>> my_list =['A1', 'A10', 'A2', 'B0', 'AA11', 'AB10']>>> sorted(my_list, key=keyfunc)['A1', 'A2', 'A10', 'AA11', 'AB10', 'B0']>>> mylist =['foo1', 'foo10', 'foo2', 'foo2bar1', 'foo2bar10', 'foo2bar3']>>> sorted(mylist, key=keyfunc)['foo1', 'foo2', 'foo2bar1', 'foo2bar3', 'foo2bar10', 'foo10']