Convert the names to tuples of integers and compare the tuples:
def splittedname(s):
return tuple(int(x) for x in s.split('.'))
splittedname(s1) > splittedname(s2)
Update: Since your names apparently can contain other characters than digits, you'll need to check for ValueError
and leave any values that can't be converted to ints unchanged:
import re
def tryint(x):
try:
return int(x)
except ValueError:
return x
def splittedname(s):
return tuple(tryint(x) for x in re.split('([0-9]+)', s))
To sort a list of names, use splittedname
as a key function to sorted
:
>>> names = ['YT4.11', '4.3', 'YT4.2', '4.10', 'PT2.19', 'PT2.9']
>>> sorted(names, key=splittedname)
['4.3', '4.10', 'PT2.9', 'PT2.19', 'YT4.2', 'YT4.11']
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…