在Python中对包含路径的列表进行排序。

时间:2017-06-23 12:15:14

标签: python python-2.7 sorting

我有一个列表paths_list,其中包含特定文件夹的文件(图像)路径。例如:

['/home/username/images/s1/4.jpg', '/home/username/images/s1/7.jpg', '/home/username/images/s1/6.jpg', '/home/username/images/s1/3.jpg', '/home/username/images/s1/5.jpg', '/home/username/images/s1/10.jpg', '/home/username/images/s1/9.jpg', '/home/username/images/s1/1.jpg', '/home/username/images/s1/2.jpg', '/home/username/images/s1/12.jpg', '/home/username/images/s1/11.jpg', '/home/username/images/s1/8.jpg']

我想按顺序排序:[/1.jpg ,2.jpg .....,/12.jpg] 无论是通过长度排序还是通过按字母顺序排列顺序排序都有帮助。应该在这做什么?

4 个答案:

答案 0 :(得分:14)

您可以将lambdaos一起使用。对于排序条件,您可以使用int首先只提取文件名(使用basename),然后您可以只删除文件名而不是扩展名(使用splitext),最后转换为>>> import os >>> l = ['/home/username/images/s1/4.jpg', '/home/username/images/s1/7.jpg', '/home/username/images/s1/6.jpg', '/home/username/images/s1/3.jpg', '/home/username/images/s1/5.jpg', '/home/username/images/s1/10.jpg', '/home/username/images/s1/9.jpg', '/home/username/images/s1/1.jpg', '/home/username/images/s1/2.jpg', '/home/username/images/s1/12.jpg', '/home/username/images/s1/11.jpg', '/home/username/images/s1/8.jpg'] >>> sorted(l, key=lambda i: int(os.path.splitext(os.path.basename(i))[0])) ['/home/username/images/s1/1.jpg', '/home/username/images/s1/2.jpg', '/home/username/images/s1/3.jpg', '/home/username/images/s1/4.jpg', '/home/username/images/s1/5.jpg', '/home/username/images/s1/6.jpg', '/home/username/images/s1/7.jpg', '/home/username/images/s1/8.jpg', '/home/username/images/s1/9.jpg', '/home/username/images/s1/10.jpg', '/home/username/images/s1/11.jpg', '/home/username/images/s1/12.jpg'] ,因此您可以按字母顺序排序,而不是按字典顺序排序。

input[type="checkbox"] {
    -ms-filter: progid:DXImageTransform.Microsoft.Matrix(M11=1.5, M12=0, M21=0, M22=1.5, SizingMethod='auto expand');
    -moz-transform: scale(1.5); /* FF */
    transform: scale(1.5); 
    padding: 5px;
}

答案 1 :(得分:6)

使用自然排序(请参阅this question):清理代码和排序字符串时的良好做法。

from natsort import natsorted
l = ['/home/username/images/s1/4.jpg', '/home/username/images/s1/7.jpg', '/home/username/images/s1/6.jpg', '/home/username/images/s1/3.jpg', '/home/username/images/s1/5.jpg', '/home/username/images/s1/10.jpg', '/home/username/images/s1/9.jpg', '/home/username/images/s1/1.jpg', '/home/username/images/s1/2.jpg', '/home/username/images/s1/12.jpg', '/home/username/images/s1/11.jpg', '/home/username/images/s1/8.jpg']
natsorted(l)

给出

['/home/username/images/s1/1.jpg',
'/home/username/images/s1/2.jpg',
'/home/username/images/s1/3.jpg',
'/home/username/images/s1/4.jpg',
'/home/username/images/s1/5.jpg',
'/home/username/images/s1/6.jpg',
'/home/username/images/s1/7.jpg',
'/home/username/images/s1/8.jpg',
'/home/username/images/s1/9.jpg',
'/home/username/images/s1/10.jpg',
'/home/username/images/s1/11.jpg',
'/home/username/images/s1/12.jpg']

自然排序根据您在计算机屏幕上(按字母顺序和数字顺序)读取内容的方式排序,而不是计算机如何读取代码。

答案 2 :(得分:1)

你可以使用拆分" /",取最后一个元素,拆分"。",取第一个,然后转换为int:

l = ['/home/username/images/s1/4.jpg', '/home/username/images/s1/7.jpg', '/home/username/images/s1/6.jpg', '/home/username/images/s1/3.jpg', '/home/username/images/s1/5.jpg', '/home/username/images/s1/10.jpg', '/home/username/images/s1/9.jpg', '/home/username/images/s1/1.jpg', '/home/username/images/s1/2.jpg', '/home/username/images/s1/12.jpg', '/home/username/images/s1/11.jpg', '/home/username/images/s1/8.jpg']
sorted_list = sorted(l, key = lambda x: int(x.split("/")[-1].split(".")[0]))

输出

['/home/username/images/s1/1.jpg',
 '/home/username/images/s1/2.jpg',
 '/home/username/images/s1/3.jpg',
 '/home/username/images/s1/4.jpg',
 '/home/username/images/s1/5.jpg',
 '/home/username/images/s1/6.jpg',
 '/home/username/images/s1/7.jpg',
 '/home/username/images/s1/8.jpg',
 '/home/username/images/s1/9.jpg',
 '/home/username/images/s1/10.jpg',
 '/home/username/images/s1/11.jpg',
 '/home/username/images/s1/12.jpg']

答案 3 :(得分:1)

这里的其他答案都很好。但无论如何我想发布一些解释

from os.path import basename,splitext
path_list = ['/home/username/images/s1/4.jpg', '/home/username/images/s1/7.jpg',
             '/home/username/images/s1/6.jpg', '/home/username/images/s1/3.jpg',
             '/home/username/images/s1/5.jpg', '/home/username/images/s1/10.jpg',
             '/home/username/images/s1/9.jpg', '/home/username/images/s1/1.jpg',
             '/home/username/images/s1/2.jpg', '/home/username/images/s1/12.jpg',
             '/home/username/images/s1/11.jpg', '/home/username/images/s1/8.jpg']

new_list = [splitext(basename(x))[0] for x in path_list]

fin_list = list(zip(path_list,new_list))

fin_list = [x[0] for x in sorted(fin_list,key=lambda x: int(x[1]))]

print(fin_list)

1)创建一个只包含文件名的列表。 1,2,..等等。

new_list = [splitext(basename(x))[0] for x in path_list]

注意:为什么[0]?因为每个splitext(basename(x))[0]的输出都是这样的,

('1','.jpg') , ('4','.jpg')

所以[0] 0th索引只给我们文件名!

2)压缩来自两个可迭代的每个项目并创建一个列表。所以这个列表有这样的值,

fin_list = list(zip(path_list,new_list))
#output
('/home/username/images/s1/4.jpg','4.jpg')

3)[x[0] for x in sorted(fin_list,key=lambda x: int(x[1]))]

这个从fin_list注释列表中创建一个列表是这里的主要内容。密钥将是来自元组的第二项,即4,3,7,..等。根据发生的排序。

最后你的输出

['/home/username/images/s1/1.jpg', '/home/username/images/s1/2.jpg',
 '/home/username/images/s1/3.jpg', '/home/username/images/s1/4.jpg',
 '/home/username/images/s1/5.jpg', '/home/username/images/s1/6.jpg', 
'/home/username/images/s1/7.jpg', '/home/username/images/s1/8.jpg',
 '/home/username/images/s1/9.jpg', '/home/username/images/s1/10.jpg',
 '/home/username/images/s1/11.jpg', '/home/username/images/s1/12.jpg']