Let's say I have two lists of files with similar names like so:
images = ['image_im_1', 'image_im_2']
masks = ['mask_im_1', 'mask_im_2', 'mask_im_3']
How would I be able to efficiently remove elements that aren't matching? I want to get the following:
images = ['image_im_1', 'image_im_2']
masks = ['mask_im_1', 'mask_im_2']
I've tried doing the following:
setA = set([x[-4:] for x in images])
setB = set([x[-4:] for x in masks])
matches = setA.union(setB)
elems = list(matches)
for elem in elems:
result = [x for x in images if x.endswith(elem)]
But this is rather naïve and slow as I need to iterate through a list of ~100k elements. Any idea how I can effectively implement this?