python - 如何按数字顺序导入图像?
问题描述
我有这段代码可以从文件夹中导入图像,但图像按以下顺序排列:
test1.png
test10.png
test100.png
test101.png
test102.png
test103.png
test104.png
test105.png
test106.png
test107.png
test108.png
test109.png
test11.png
test110.png
test111.png
test112.png
test113.png
test114.png
test115.png
test116.png
test117.png
test118.png
test119.png
test12.png
etc...
我想要的顺序是test1
, test2
, test3
, 等等...
我怎样才能做到这一点?
test_set = []
test_result=[]
test_dir= "C:/Users/anwer/Desktop/copy/test/"
for file in os.listdir(test_dir):
test_set.append((give_peak_sum(test_dir+file), file))
test_result.append((give_peak_sum(test_dir+file)))
print(file)
解决方案
您需要先对列表进行排序。
如果您的所有文件都以“测试”开头
你可以使用
yourlist.sort(key=lambda x: int(x.split('.')[0][4:]))
推荐阅读
- angular - 使用快照或订阅从 Angular activeRoute 捕获查询参数的更好方法是什么?
- javascript - 材质 UI 网格 没有 ID 为 #undefined 的行
- snowflake-cloud-data-platform - 将过程信息记录到雪花表中
- python - Python:迭代类属性?
- python - Tkinter python,无法从缩放对象中获取值
- sql-server - 可以从 SQL Server 数据库中的存储过程运行 Linux 脚本吗?
- csv - ANSI 编码的 CSV 文件由 VSCode 以 UTF8 重新编码。如何回到ANSI?
- node.js - 使用 babel NodeJs 进行模块化
- oauth-2.0 - 特定于资源的 OAuth2 授权的常见做法?
- python - Numpy 切片数组内存消耗