次のリストがあります:
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)
ただし、一部の文字列には数字がないため、エラーが発生します。ありがとう。