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!!
CodePudding user response:
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.
CodePudding user response:
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)))