我需要清理我的存档文件夹,以便只保留按发布类型和版本ID排序的前两个文件。
我将文件名拆分为一个列表并尝试使用operator.itemgetter,但多次排序是一项代价高昂的操作。
示例:
1.0.0-release-735586.zip
1.0.0-release-447088.zip
1.0.0-release-436044.zip
1.0.1-patch-480506.zip
1.0.1-patch-968125.zip
1.0.1-patch-532147.zip
应该是
1.0.0-release-735586.zip
1.0.0-release-447088.zip
1.0.1-patch-968125.zip
1.0.1-patch-532147.zip
答案 0 :(得分:1)
from itertools import groupby
li = ['1.0.0-release-735586.zip', '1.0.0-release-447088.zip', '1.0.0-release-436044.zip',
'1.0.1-patch-480506.zip', '1.0.1-patch-968125.zip', '1.0.1-patch-532147.zip']
new= []
for key, group in groupby(li, lambda x: x[:-10]):
new.extend(sorted(list(group),reverse=True)[:2])
在这里,我们可以通过排除最后10个字符来对列表元素进行分组,即'1.0.0-release-735586.zip'[:-10]
将给出'1.0.0-release-'
。
对于每个组,我们只按前两个元素按降序排序。这由sorted
和[:2]
new
['1.0.0-release-735586.zip',
'1.0.0-release-447088.zip',
'1.0.1-patch-968125.zip',
'1.0.1-patch-532147.zip']