简体   繁体   中英

how to sort a string list with integer and no separator

I'm trying to sort this list. My files looks like this: "crown"+"t"+"0.01" I would like to sort this list in function of t with t increasing. Thanks you.

import glob

txtfiles = []
for file in glob.glob("crown*0.01"):
    txtfiles.append(file)
print(txtfiles)

output:

['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() function will work. You can try this:-

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)

Output:-

['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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM