[英]Python: How to get the same sorting order as Windows file name sorting?
[英]Python glob: sorting files of format [int]_[int] the same as windows name sort
我有在Windows中按名稱排序的文件,如下所示:
1_0
1_1
1_2
1_3
1_4
1_5
1_6
1_7
1_8
1_9
1_10
1_11
2_0
2_1
在_
之前的第一個整數之上的Per應該是第一個排序,然后在_
之后的整數。 我想以相同的方式對glob進行排序。 但是,當我使用sorted(glob.glob(files))
我得到的命令如下:
1_1
1_10
1_11
1_12
1_13
1_14
1_15
1_16
1_17
1_18
1_19
1_2
對於上述格式的文件,是否有一種簡單的方法可以像Windows一樣通過名稱進行glob排序?
您可以按相關順序將多個鍵分配給排序功能
sorted(glob.glob(files), key=lambda x: (int(x.split('_')[0]), int(x.split('_')[1])))
['1_0','1_1','1_2','1_3','1_4','1_5','1_6','1_7','1_8','1_9','1_10','1_11',' 2_0',“ 2_1”]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.