python下使用sort()函數對目錄下文件名進行多條件排序


1.基礎函數

  • a.sort()函數
    sort()函數的作用是對列表內容進行正向排序,直接在原列表進行修改,返回的是修改后的列表。
lists =[1, 5, 10, 8, 6]
lists.sort() 
print(lists) 
>>> [1, 5, 6, 8, 10]
file_list=['file1', 'file101', 'file102', 'file103', 'file11', 
'file12','file13', 'file2', 'file21', 'file22', 'file23', 'file3']
# 文件夾名 按數字排序
file_list.sort(key=lambda x: int(x[4:]))  # [4:]表示從字符第4位后開始排序
print(file_list) 
# ['file1', 'file2', 'file3', 'file11', 'file12', 'file13',
# 'file21', 'file22', 'file23', 'file101', 'file102', 'file103']
  • b.sorted()函數
    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]
  • c.多條件排序
    進行多條件排序的話,用sort()函數或者sorted()函數都可以。例子這邊用的sort()函數,使用參數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)]

2.例子解析

我需要的是把Mframes0_EPI1.png,Mframes0_EPI10.png,Mframes0_EPI2.png,Mframes1_EPI10.png,Mframes10_EPI10.png,Mframes2_EPI10.png,Mframes2_EPI2.png這樣一系列的文件名進行排序。在字符型的數字比較中,10是排在2前面的,即Mframes0_EPI10.png的排序在Mframes0_EPI2.png前面,而我需要的是將Mframes0_EPI2.png排在Mframes0_EPI10.png前面。如果單純的數字比較的話,直接加個int轉化為整型即可實現,但是這邊的文件名不單包含了數字還包含了字母和格式。這就需要對文件名進行分割再排序的處理。

  • split()函數
    split()函數的作用是將一個字符串分裂成多個字符串組成的列表。
L = '+100+200+300+400+500+'
print(L.split('+')) # 以''+''號為分割符
# 執行結果:['', '100', '200', '300', '400', '500', '']
  • 使用split()函數進行多符號分割
    調用re模塊中的split()函數可以用多個符號進行分割。
import re
words = '我,來。上海?吃?上海菜'
wordlist = re.split(',|。|?',words)
print(wordlist)
# output:['我', '來', '上海', '吃', '上海菜']
  • 對文件名進行分割再排序
    再回過頭看例子。因為文件名Mframes0_EPI1.png中包含字母和格式,無法對其整體取整型int()處理排序。所以需要對文件名進行分割后排序
    在這里插入圖片描述
    如上圖所示,通過re.split()函數進行多符號分割,將Mframes0_EPI1.png中的Mframes_EPI.png去掉,然后通過sort()函數將留下的數字01取整型int(),最后按第一優先級和第二優先級進行排序(也就是先排0這邊的數字,再排1那邊的數字)。
    完整代碼如下:
valid_hr_img_list = ['Mframes0_EPI1', 'Mframes0_EPI10', 'Mframes0_EPI11', 'Mframes0_EPI12',
'Mframes0_EPI13', 'Mframes0_EPI14', 'Mframes0_EPI15', 'Mframes0_EPI16','Mframes0_EPI17',
'Mframes0_EPI18', 'Mframes1_EPI10', 'Mframes3_EPI10', 'Mframes4_EPI10', 'Mframes399_EPI96', 
'Mframes0_EPI19', 'Mframes0_EPI2', 'Mframes0_EPI20']
valid_hr_img_list.sort(key=lambda x:  (int(re.split('Mframes|_EPI|.png',x)[1]),int(re.split('Mframes|_EPI|.png',x)[2])))
print(valid_hr_img_list)
# ['Mframes0_EPI1', 'Mframes0_EPI2', 'Mframes0_EPI10', 'Mframes0_EPI11', 'Mframes0_EPI12', 
# 'Mframes0_EPI13', 'Mframes0_EPI14', 'Mframes0_EPI15', 'Mframes0_EPI16', 'Mframes0_EPI17',
# 'Mframes0_EPI18', 'Mframes0_EPI19', 'Mframes0_EPI20', 'Mframes1_EPI10', 'Mframes3_EPI10', 
# 'Mframes4_EPI10', 'Mframes399_EPI96']

可以清楚看到排序后的結果:Mframes0_EPI2.png是排在Mframes0_EPI10.png前面的,排序正確。

參考

[1] Python文件名排序或文件排序問題
[2] 文件名(文件夾名) 排序: 按字符串排序、 按數字排序 Python
[3] python對目錄下的文件進行 多條件排序
[4] Python之split()函數
[5] 在python 中split()使用多符號分割的例子
碼字不易,如果您覺得有幫助,麻煩點個贊再走唄~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM