Is there a built-in function to sort and filter a python list in one step?
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 r开发者_开发百科ange
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
files = sorted( (f for f in files if firstFile <= int(f) < lastFile), key=int)
The most straightforward way to do this is (in 2.6 at least) is to create a filtering generator using itertools.ifilter
and then sort it:
>>> from itertools import ifilter
>>> seq = [ 1, 43, 2, 10, 11, 91, 201]
>>> gen = ifilter(lambda x: x % 2 == 1, seq)
>>> sorted(gen)
[1, 11, 43, 91, 201]
The underlying sequence doesn't get traversed and filtered until sorted
starts iterating over it.
精彩评论