당신은 human sorting을 찾고 있습니다.
import re
# Source: http://nedbatchelder.com/blog/200712/human_sorting.html
# Author: Ned Batchelder
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
"""
return [ tryint(c) for c in re.split('([0-9]+)', s) ]
def sort_nicely(l):
""" Sort the given list in the way that humans expect.
"""
l.sort(key=alphanum_key)
data=[
'title_text #49',
'title_text #5',
'title_text #50']
sort_nicely(data)
print(data)
# ['title_text #5', 'title_text #49', 'title_text #50']
편집 :
data=[{'title': 'title_text #49', 'x':0},
{'title':'title_text #5', 'x':10},
{'title': 'title_text #50','x':20}]
data.sort(key=lambda x: alphanum_key(x['title']))
# [{'x': 10, 'title': 'title_text #5'}, {'x': 0, 'title': 'title_text #49'}, {'x': 20, 'title': 'title_text #50'}]
언제든지 왼쪽에 숫자 0을 채울 수 있습니까? 예를 들어 title_text # 5가'title_text # 05' - 그럼 당신은 '픽스'할 필요가 없습니다. –