python-3.x - 按数字对图像文件名列表进行排序
问题描述
我正在尝试订购以下图像序列列表。即frame0.jpg --> frame17.jpg
:
我曾尝试拆分名称,然后对它们进行排序,但它不起作用。
这是我的代码:
Data = [
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame0.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame1.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame10.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame11.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame12.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame13.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame14.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame15.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame16.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame17.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame2.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame3.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame4.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame5.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame6.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame7.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame8.jpg",
"D:\\shooting_videos\\example\\Output\\trained_framesmake2\\frame9.jpg",
]
sorted_Data = sorted(Data, key=lambda x: int(x.split('.')[0]))
解决方案
您可以使用 regex (\d+)\.[a-z]+$
,它在文件扩展名之前捕获数字,假设是按字母顺序排列的。
import re
sorted(data, key=lambda x: int(re.search(r"(\d+)\.[a-z]+$", x).group(1)))
推荐阅读
- python - Seaborn barplot 带有第三个变量的标签条
- java - 使用弹簧启动在 rsocket 消息上设置标头
- vue.js - 如何在存储对象中访问父 vue 页面?
- java-8 - 是否可以匹配 Map 的多个键而不是 forEach 中的 IF 语句?
- node.js - 如何在express js中将空白字段更改为null
- docker - 如何将文件传递到 Docker 容器中以与容器一起使用?
- sql - Vb6 和访问日期范围
- ios - 我如何知道我从其他应用程序快速返回
- javascript - 在我的应用程序的不同屏幕之间设置导航
- reactjs - 如何在系列折线图Highcharts Reactjs中获得2个具有一个名称的数据点