python - Sort list of strings by digits appearing in each element -


this question has answer here:

i have script purpose sort , process spatial dataset files being downloaded onto server. list looks this:

list = ['file.t00z.wrff02.grib2', 'file.t00z.wrff03.grib2', 'file.t00z.wrff00.grib2', 'file.t00z.wrff05.grib2', 'file.t00z.wrff04.grib2', 'file.t00z.wrff01.grib2', 'file.t06z.wrff01.grib2', 'file.t06z.wrff00.grib2', 'file.t06z.wrff02.grib2', ...]

as can see, each file has specific naming convention.

later in script, files in list processed sequentially, need process them in order of time designated 2 digits following "wrff" in each filename (00, 01, 02...).

i have regular expression removes files list don't match 2 digits following "file.t", necessary. there easy method sort list elements substring?

note: choose sort these files modification time, appear in data directory out of order.

you can use sorted or sort , supply lambda function extracts numbers want key.

sorted_list = sorted(list, key=lambda f: f[f.find('wrff'): f.find('wrff') + 6]) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -