当前位置: 移动技术网 > IT编程>脚本编程>Python > python对目录下的文件进行 多条件排序

python对目录下的文件进行 多条件排序

2019年06月07日  | 移动技术网IT编程  | 我要评论

逃脱本色2-11攻略,爱滋病初期症状,上海市妇幼保健院

在进入正题之前,先介绍一下基础知识:

1、sort(),方法:就是对列表内容进行正向排序,直接在原列表进行修改,返回的是修改后的列表

lists =[1, 5, 10, 8, 6]
lists.sort()
print(lists)
>>> [1, 5, 6, 8, 10]


2、sorted() 方法: 对列表进行排序后,返回一个新的列表,而原列表不变。并且sorted()方法可以用在任何数据类型的序列中,而返回的总是一个列表的形式。

lists = [1, 5, 10, 8, 6]
a = sorted(lists)
print(lists)
>>>[1, 5, 10, 8, 6]
print(a)
>>>[1, 5, 6, 8, 10]

3、进行多条件排序,使用参数 key 即可,其返回的顺序就是按照元组的顺序 。如果想要第一个顺序,第二个逆序,只需要在 x[1] 前面加上 -x[1]

lists = [(2, 5), (2, 3), (1, 2), (4, 2), (3, 4)]

lists.sort(key=lambda x: (x[0], x[1]))

print(lists)
>>>[(1, 2), (2, 3), (2, 5), (3, 4), (4, 2)]

   好,介绍完之后,下面进入正题,自定义顺序 读取文件 多条件排序。

   如图,要让下面这些文件进行自己想要的顺序排序,首先根据月份排,然后依据日期排;即先五月,从 5_1 到 5_15,然后再到 6_1 ,如果只是单纯的采用 sort() 和sorted()肯定是不能实现的,需要自定义方式,进行排序。

 

那么怎么来排序呢?

思路就是: 先对文件进行分割,得到 月份 和 天数,然后利用sort() 的key值,进行排序。

import os


path = '..\\url\\'
file_selected = os.listdir(path)
month = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']

file_tem = []
file_comp = []
for file in file_selected:
    file_tem.append(file)    # [file]
    value = file.split("_")[1].split(" ")[0]
    ind = month.index(value)  # 得到月份的下标
    file_tem.append(ind)      # [file,ind]
    num = int(file.split("_")[1].split(" ")[1])  # 得到天数
    file_tem.append(num)      # [file,ind,num]
    file_comp.append(file_tem)  # 得到[[file,ind,num],[file2,ind2,num2]]的形式
    file_tem = []

file_comp.sort(key=lambda x: (x[1], x[2]))  # 根据ind排,再根据num2 排
sorted_file = [x[0] for x in file_comp]

print(sorted_file)

最终得到结果:

['comprehensive risk report_may 1_ 2019 9-00-36 am 076.html',
 'comprehensive risk report_may 2_ 2019 9-00-36 am 076.html',
 'comprehensive risk report_may 3_ 2019 9-00-40 am 593.html',
 'comprehensive risk report_may 4_ 2019 9-00-46 am 963.html',
 'comprehensive risk report_may 5_ 2019 9-00-50 am 724.html',
 'comprehensive risk report_may 6_ 2019 9-00-53 am 563.html',
 'comprehensive risk report_may 7_ 2019 9-00-54 am 080.html',
 'comprehensive risk report_may 8_ 2019 9-00-37 am 000.html',
 'comprehensive risk report_may 9_ 2019 9-00-37 am 935.html',
 'comprehensive risk report_may 10_ 2019 9-00-39 am 314.html',
 'comprehensive risk report_may 11_ 2019 9-00-40 am 031.html',
 'comprehensive risk report_may 12_ 2019 9-00-42 am 145.html',
 'comprehensive risk report_may 13_ 2019 9-00-43 am 490.html',
 'comprehensive risk report_may 14_ 2019 9-00-13 am 544.html',
 'comprehensive risk report_may 15_ 2019 9-00-23 am 408.html',
 'comprehensive risk report_jun 1_ 2019 9-00-27 am 541.html']

这里为什么采用  file_tem = [ ] 而不是采用 file_tem.clear(),将在下一篇介绍。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网