I have a list of substrings and a list of strings. I would like to find all matching substrings in the list of strings. When substrings are found in the strings I would like to create a new list of strings containing all substring matches found in each string.
For example let's say I have these:
substrings = ["word","test"]
strings = ["word string one", "string two test", "word and test", "no matches in this string"]
I have created the following to match the substrings with the string:
for s in strings:
for k in substrings:
if k in s:
print(k)
This give the following output:
word
test
word
test
I have also tried the following:
matches = [x for string in strings for x in string.split() if x in substrings]
print (matches)
output:
['word', 'test', 'word', 'test']
None of these results are what I am after. As both "word" and "test" occur in the third string I would like to get something similar to either of the following outputs:
word
test
word, test
or
['word', 'test', 'word test']