Check if strings from list A exist in list B (by regex)

Question:

listA = ['Leonardo_da_Vinci', 'Napoleon', 'Cao_Cao', 'Elton_John']
listB = ['123_Leonardo_da_Vinci_abc.csv', '456_Cao_Cao_def.csv']

listC = ['Napoleon', 'Elton_John']

I would like to check if the items in listB contain the values in listA, and returns listC (i.e. the list that is missing in listB). For this purpose I would like to use regex (and no other heuristics) like (example of checking Leonardo_da_Vinci): .*Leonardo_da_Vinci.*

The reason for regex is that the example above is just the simplest mockup and the data I use is much bigger. It is also good to have a generalised code which works on another data in future.

Asked By: user7665853

||

Answers:

Something like this:

import re

def exists_csv_with_name(name:str, source_list: list) -> bool:
    regex = re.compile(fr'.*{name}.*')
    return any(regex.match(source_str) for source_str in source_list)
    
listC = [name for name in listA if not exists_csv_with_name(name, listB)]
Answered By: Jorge Luis
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.