I have a list of path file
image_list1 = ['/content/gdrive/MyDrive/Match/bed/2.jpg','/content/gdrive/MyDrive/Match/bed/4.jpg']
and
image_list1=['/content/gdrive/MyDrive/Match/chairs/4.jpg','/content/gdrive/MyDrive/Match/chairs/3.jpg','/content/gdrive/MyDrive/Match/chairs/2.jpg','/content/gdrive/MyDrive/Match/chairs/1.jpg']
what is the code How I can create another list like this line
image_list3=['/content/gdrive/MyDrive/Match/chairs/2.jpg','/content/gdrive/MyDrive/Match/chairs/4.jpg']
I have two folders beds and chairs?
CodePudding user response:
Try:
bed_list = [
"/content/gdrive/MyDrive/Match/bed/2.jpg",
"/content/gdrive/MyDrive/Match/bed/4.jpg",
]
chair_list = [
"/content/gdrive/MyDrive/Match/chairs/4.jpg",
"/content/gdrive/MyDrive/Match/chairs/3.jpg",
"/content/gdrive/MyDrive/Match/chairs/2.jpg",
"/content/gdrive/MyDrive/Match/chairs/1.jpg",
]
tmp = {i.rsplit("/", maxsplit=1)[-1] for i in bed_list}
out = [i for i in chair_list if i.rsplit("/", maxsplit=1)[-1] in tmp]
print(out)
Prints:
[
"/content/gdrive/MyDrive/Match/chairs/4.jpg",
"/content/gdrive/MyDrive/Match/chairs/2.jpg",
]