'How to get the regex match without None

I am trying to find pattern matching with Regex. The used case is as follows:

listStrings1 = ['abc','def', 'ghi']
listSubstrings1 = ['a', 'b', 'e']

listStrings1 is the strings inputs and listSubstrings1 is the pattern which needed to be matched. so output should look like

['a', 'b'], ['b'], []

but currently I am getting like

['a', 'b', None], ['b', None, None], [None, None, None]

Here is what I tried so far

def research(substring, string):
    match = re.search(substring, string)
    emptystr = ''
    if match is not None:
        return emptystr
    else:
        return substring

def substringsearch(sublist, string):
    matchlist = list(map(lambda y: research(y, string), sublist ))
    return matchlist

listm1 = list(map(lambda x: substringsearch(listSubstrings1, x), listStrings1))
print(listm1)

Also time is another crucial factor as size of string input and substring input is around 100k so if possible without using loops.

Any help is appreciated. Thanks!!



Solution 1:[1]

I'm not sure about how optimized is this solution but its working and easy to understand and implement.

listStrings1 = ['abc', 'def', 'ghi']
listSubstrings1 = ['a', 'b', 'e']

all_matches = []
for s in listStrings1:
    matches = []
    for x in listSubstrings1:
        if x in s:
            matches.append(x)
    all_matches.append(matches)

print(all_matches)

output:

[['a', 'b'], ['e'], []]

this code can be simplify into one-liner but I think its easier to understand this way.

Solution 2:[2]

Simply use the built-in filter() function on the resulting map object before converting it to a list:

listm1 = list(filter(None, map(lambda x: substringsearch(listSubstrings1, x), listStrings1)))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Uri Datz
Solution 2 Ann Zen