我有下一个清单:
a = ['1th Word', 'Another Word', '10th Word']
print a.sort()
>>> ['10th Word', '1th Word', 'Another Word']
但是我需要:
['1th Word', '10th Word','Another Word']
是否有捷径可寻?
我试过:
r = re.compile(r'(\d+)')
def sort_by_number(s):
m = r.match(s)
return m.group(0)
x.sort(key=sort_by_number)
但是有些字符串没有数字,这会导致错误。谢谢。