Given a directory of files all with numeric names, I currently sort and filter the directory list in two steps.
#files = os.listdir(path)
files = ["0", "1", "10", "5", "2", "11", "4", "15", "18", "14", "7", "8", "9"]
firstFile = 5
lastFile = 15
#filter out any files that are not in the desired range
files = filter(lambda f: int(f) >= firstFile and int(f) < lastFile, files)
#sort the remaining files by timestamp
files.sort(lambda a,b: cmp(int(a), int(b)))
Is there a python function that combines the filter and sort operations so the list only needs to be iterated over once?
Those are orthogonal tasks, I don’t think they should be mixed. Besides, it’s easy to filter and sort separately in one line with generator expressions