相关疑难解决方法(0)

是否有一个内置函数可以一步对python列表进行排序和过滤?

给定一个带有数字名称的文件目录,我目前分两步对目录列表进行排序和过滤.

#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

8
推荐指数
1
解决办法
9231
查看次数

标签 统计

python ×1