我想从一个路径读取数据集:
from os import listdir
import re
dir = listdir('F:/dataset/TNO_test/ir')
print(dir)
结果是:
['1.bmp', '10.bmp', '11.bmp', '12.bmp', '13.bmp', '14.bmp', '15.bmp', '16.bmp', '17.bmp', '18.bmp', '19.bmp', '2.bmp', '20.bmp', '3.bmp', '4.bmp', '5.bmp', '6.bmp', '7.bmp', '8.bmp', '9.bmp']
我希望图片能够以“1.bmp、2.bmp、3.bmp…”的顺序排列,所以调用了列表的sort方法:
dir.sort()
print(dir)
结果却还是:
['1.bmp', '10.bmp', '11.bmp', '12.bmp', '13.bmp', '14.bmp', '15.bmp', '16.bmp', '17.bmp', '18.bmp', '19.bmp', '2.bmp', '20.bmp', '3.bmp', '4.bmp', '5.bmp', '6.bmp', '7.bmp', '8.bmp', '9.bmp']
原因在于Python的字符串排序是逐字符比较ASCII值,比如为什么‘10.bmp’会排在‘2.bmp’前面?因为从第一个字符来看,‘1’是小于‘2’的。解决的办法就是将数字提取出来,按照数值大小排序:
dir.sort(key=lambda x: int(re.findall(r'\d+', x)[0]))
print(dir)
找到文件名中所有连续的数字串,并返回第一个数字片段,转为整数类型
['1.bmp', '2.bmp', '3.bmp', '4.bmp', '5.bmp', '6.bmp', '7.bmp', '8.bmp', '9.bmp', '10.bmp', '11.bmp', '12.bmp', '13.bmp', '14.bmp', '15.bmp', '16.bmp', '17.bmp', '18.bmp', '19.bmp', '20.bmp']