给定一个带有数字名称的文件目录,我目前分两步对目录列表进行排序和过滤.
#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)))
Run Code Online (Sandbox Code Playgroud)
是否有一个python函数结合了筛选和排序操作,所以列表只需要迭代一次?
python ×1