Python 如何使用整数和无分隔符对字符串列表进行排序
我正试着对这个列表进行排序。我的文件如下所示:“crown”+“t”+“0.01” 我想用t随t的增加对这个列表进行排序。 谢谢你Python 如何使用整数和无分隔符对字符串列表进行排序,python,list,sorting,Python,List,Sorting,我正试着对这个列表进行排序。我的文件如下所示:“crown”+“t”+“0.01” 我想用t随t的增加对这个列表进行排序。 谢谢你 import glob txtfiles = [] for file in glob.glob("crown*0.01"): txtfiles.append(file) print(txtfiles) 输出: ['crown0.90.01', 'crown0.250.01', 'crown0.550.01', 'crown0.650.
import glob
txtfiles = []
for file in glob.glob("crown*0.01"):
txtfiles.append(file)
print(txtfiles)
输出:
['crown0.90.01', 'crown0.250.01', 'crown0.550.01', 'crown0.650.01', 'crown1.90.01', 'crown2.10.01', 'crown0.850.01', 'crown0.20.01', 'crown0.80.01', 'crown0.10.01']
sort()
函数将起作用。您可以尝试以下方法:-
l = ['crown0.90.01', 'crown0.250.01', 'crown0.550.01', 'crown0.650.01', 'crown1.90.01', 'crown2.10.01', 'crown0.850.01', 'crown0.20.01', 'crown0.80.01', 'crown0.10.01']
l.sort()
print(l)
输出:-
['crown0.10.01', 'crown0.20.01', 'crown0.250.01', 'crown0.550.01', 'crown0.650.01', 'crown0.80.01', 'crown0.850.01', 'crown0.90.01', 'crown1.90.01', 'crown2.10.01']
非常感谢。我不认为这个函数能够做到这一点。。。