Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select between lists elements with regex

I have 2 lists:

images = ['ND_row_id_4026.png',
          'ND_row_id_7693.png',
          'ND_row_id_5285.png',
          'ND_row_id_1045.png',
          'ND_row_id_2135.png',
          'ND_row_id_8155.png',
          'ND_row_id_3135.png']

masks = ['MA_row_id_4026.png',
         'MA_row_id_7693.png',
         'MA_row_id_5285.png',
         'MA_row_id_1045.png',
         'MA_row_id_2135.png']

I want to keep the masks files. So, I have 2 folders with images and masks and I want to keep all the masks that are in the masks list and the corresponding images. The images list is bigger than the masks.

So ,I want to keep from the images list:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

images = ['ND_row_id_4026.png',
          'ND_row_id_7693.png',
          'ND_row_id_5285.png',
          'ND_row_id_1045.png',
          'ND_row_id_2135.png']

>Solution :

It seems (based on your sample data) a simple list comprehension should suffice, just replacing the prefix for comparison:

[im for im in images if im.replace('ND', 'MA') in masks]

Output (for your sample data):

[
 'ND_row_id_4026.png',
 'ND_row_id_7693.png',
 'ND_row_id_5285.png',
 'ND_row_id_1045.png',
 'ND_row_id_2135.png'
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading