I'm processing some files in a directory and need the files to be sorted numerically. I found some examples on sorting--specifically with using the lambda
pattern--at wiki.python.org, and I put this together:
#!env/python
import re
tiffFiles = """ayurveda_1.tif
ayurveda_11.tif
ayurveda_13.tif
ayurveda_2.tif
ayurveda_20.tif
ayurveda_22.tif""".split('
')
numPattern = re.compile('_(d{1,2}).', re.IGNORECASE)
tiffFiles.sort(cmp, key=lambda tFile:
int(numPattern.search(tFile).group(1)))
print tiffFiles
I'm still rather new to Python and would like to ask the community if there are any improvements that can be made to this: shortening the code up (removing lambda
), performance, style/readability?
Thank you,
Zachary
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…