[英]How do I sort the filenames for a dataset, I got the names using glob.glob('path/*.png')
Hi.你好。 I am trying to load the dataset, I want to get the filenames sorted as 1,2,3..., But I am getting them as 1, 10,100,1000.... as you can see in the images?
我正在尝试加载数据集,我想将文件名排序为 1、2、3 ...,但我将它们作为 1、10、100、1000 ....,正如您在图像中看到的那样? How should I get it done?
我应该如何完成它?
filenames = glob.glob("/content/path/train/*.png")
filenames.sort()
images = [cv2.imread(img) for img in filenames]
Well, that IS alphabetical order... If you know the names are all numeric, then you can supply a key
function to .sort()
that uses the ordering you want.嗯,这是字母顺序...如果您知道名称都是数字,那么您可以提供一个
key
function 到.sort()
使用您想要的顺序。
def bynumber(fn):
return int(os.path.basename(fn)[:-4])
...
filenames.sort( key=bynumber )
Alternatively:或者:
filenames.sort( key=lambda fn: int(os.path.basename(fn)[:-4]) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.