这是我的输入内容:
a = ['Dr1', 'Dr10', 'Dr11', 'Dr12','Dr2', 'Dr3']
a.sort()
print(a)
以下是当前输出:
['Dr1', 'Dr10', 'Dr11', 'Dr12', 'Dr2', 'Dr3']
这是我想生成的输出:
['Dr1', 'Dr2', 'Dr3', 'Dr10', 'Dr11', 'Dr12']
答案 0 :(得分:0)
为了通过每个元素中的尾随值进行排序,请使用re
:
import re
a = ['Dr1', 'Dr10', 'Dr11', 'Dr12','Dr2', 'Dr3']
new_a = sorted(a, key=lambda x:int(re.findall('\d+', x)[0]))
输出:
['Dr1', 'Dr2', 'Dr3', 'Dr10', 'Dr11', 'Dr12']