如何在比较来自 2 个不同目录的图像时保持 python 中的顺序?

How to preserve order in python while comparing images from 2 different directories?

我正在比较来自 2 个不同目录的图像。我写代码但是比较的顺序是这样

{ 0,1,10,11,12,...,19,2,20,21,..} 不像 {0,1,2,3,...,9,10, 11,12,...}

L1 = os.listdir("D:\image_dir_1\")

image_list_1 = list()
image_list_2 = list()
final_i = list()
final_j = list()
dirs = -1 

for i in L1:
    dirs = dirs + 1
    img1 = cv2.imread("D:\image_dir_1\" +i )
    L2 = os.listdir("D:\image_dir_2\"+str(dirs) +'\')
    for j in L2:
        img2 = cv2.imread("D:\image_dir_2\" + str(dirs)+ '\' +j )
        image_list_1.append(FUN_1(img1 , img2))
        image_list_2.append(FUN_2(img1 , img2))
        final_i.append(i)
        final_j.append(j)

    filei = pd.DataFrame(final_i,columns=['Col_1'])
    filej = pd.DataFrame(final_j,columns=['Col_2'])

    frame_1 = pd.DataFrame(image_list_1,columns=['c1'])
    frame_2 = pd.DataFrame(Image_list_2,columns=['c2'])
    
    final_value = pd.concat([filei, filej, frame_1,frame_2],axis=1)

final_value.to_csv('spreadsheet.csv',index=None)

您必须对 L1 列表进行排序:

L1 = sorted(os.listdir("D:\image_dir_1\"), key=lambda str: int(str[:-4].strip()))

问题是,如果不按实际的 int 值对它们进行排序,文件名就会按字典顺序排序。