简体   繁体   English

如何按升序对目录列表进行排序?

[英]How Can I Sort a List of Directories By Ascending Order?

Background背景

I really want to rename 16,494 thousand files that all end with a site number (1 through 15,000) and a category number (1 through 8).我真的很想重命名 16,494,000 个文件,这些文件都以站点编号(1 到 15,000)和类别编号(1 到 8)结尾。 To do that I wanted to loop through the files and rename them.为此,我想遍历文件并重命名它们。 The problem is that I can't figure out how in the world to sort these files paths in the list I have.问题是我不知道如何在我拥有的列表中对这些文件路径进行排序。

First I use this to get the list of file paths in my CWD:首先,我使用它来获取我的 CWD 中的文件路径列表:

import os
import shutil
from pathlib import Path
import glob

lst = os.listdir(os.getcwd())

Then I get a list that is pretty random.然后我得到一个非常随机的列表。 It usually starts at 10,000_1.它通常从 10,000_1 开始。 I will provide a short version of the list that can work as an example.我将提供一个简短版本的列表作为示例。

lst = ['10000_1.txt','10000_2.txt','10000_3.txt','10000_4.txt','10000_5.txt','10000_6.txt','10000_7.txt','10000_8.txt',
        '1000_1.txt','1000_2.txt','1000_3.txt','1000_4.txt','1000_5.txt','1000_6.txt','1000_7.txt','1000_8.txt',
        '16494_1.txt','16494_2.txt','16494_3.txt','16494_4.txt','16494_5.txt','16494_6.txt','16494_7.txt','16494_8.txt',
        '100_1.txt','100_2.txt','100_3.txt','100_4.txt','100_5.txt','100_6.txt','100_7.txt','100_8.txt',
        '1_1.txt','1_2.txt','1_3.txt','1_4.txt','1_5.txt','1_6.txt','1_7.txt','1_8.txt']

In short we have 5 sites here with 8 category numbers: 1_(1 through 8), 100_(1 through 8), 1000_(1 through 8), 10000_(1 through 8), and 16494_(1 through 8).简而言之,我们这里有 5 个站点,有 8 个类别编号:1_(1 到 8)、100_(1 到 8)、1000_(1 到 8)、10000_(1 到 8)和 16494_(1 到 8)。 They are all.txt.它们都是.txt。

What I tried我试过的

lst = lst.sort()
print(lst)

I don't know what to do.我不知道该怎么办。 I have tried other things, but I don't get anything or it doesn't sort anything.我已经尝试过其他东西,但我什么也没得到,或者它没有对任何东西进行排序。 I want it to look like this:我希望它看起来像这样:

What I want我想要的是

lst = ['1_1.txt','1_2.txt','1_3.txt','1_4.txt','1_5.txt','1_6.txt','1_7.txt','1_8.txt',
        '100_1.txt','100_2.txt','100_3.txt','100_4.txt','100_5.txt','100_6.txt','100_7.txt','100_8.txt',
        '1000_1.txt','1000_2.txt','1000_3.txt','1000_4.txt','1000_5.txt','1000_6.txt','1000_7.txt','1000_8.txt',
        '10000_1.txt','10000_2.txt','10000_3.txt','10000_4.txt','10000_5.txt','10000_6.txt','10000_7.txt','10000_8.txt',
        '16494_1.txt','16494_2.txt','16494_3.txt','16494_4.txt','16494_5.txt','16494_6.txt','16494_7.txt','16494_8.txt']

Any help would be appreciated!任何帮助,将不胜感激!

You need to use a custom key for the sorting:您需要使用自定义键进行排序:

>>> sorted(lst, key=lambda x: (int(x.split("_")[0]), int(x.split("_")[1].split(".")[0])))

Or:或者:

>>> sorted(lst, key=lambda x: tuple(map(int, x.rstrip(".txt").split("_"))))

You can simply use the split string as key:您可以简单地使用拆分字符串作为键:

sorted(lst, key=lambda x: x.split('_'))

Output: Output:

['1_1.txt', '1_2.txt', '1_3.txt', '1_4.txt', '1_5.txt', '1_6.txt', '1_7.txt', '1_8.txt', '100_1.txt', '100_2.txt', '100_3.txt', '100_4.txt', '100_5.txt', '100_6.txt', '100_7.txt', '100_8.txt', '1000_1.txt', '1000_2.txt', '1000_3.txt', '1000_4.txt', '1000_5.txt', '1000_6.txt', '1000_7.txt', '1000_8.txt', '10000_1.txt', '10000_2.txt', '10000_3.txt', '10000_4.txt', '10000_5.txt', '10000_6.txt', '10000_7.txt', '10000_8.txt', '16494_1.txt', '16494_2.txt', '16494_3.txt', '16494_4.txt', '16494_5.txt', '16494_6.txt', '16494_7.txt', '16494_8.txt']

Another alternative is to use natsorted另一种选择是使用natsorted

from natsort import natsorted
natsorted(lst)
def sort_rule(filename):
    return filename.split('_')[0]

list.sort(key=sort_rule)

I think all you need to do is look for the site number.我认为您需要做的就是查找站点编号。 The values that have the same site number will be sorted by category.具有相同站点编号的值将按类别排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM