I have a list of keywords which I wish to match in a list of sentences. If found within that sentence than return the found keyword in a list.
What I have tried:
sentence = df['List of Content']
list_of_words = ['keyword1','keyword2', 'keyword3']
This below works if I choose only one row:
[word for word in list_of_words if word in sentence[0]
and outputs
output: ['keyword1', 'keyword3']
The desirable output for all the rows, is a list of keywords that match in the sentence. Something like this:
matching_keywords = [['keyword1', 'keyword3'],['keyword2, 'keyword3'],['keyword1', 'keyword2']..]
However, when I run the for
loop in the entire list it just outputs an empty list []
I have also tried a nested for loop:
kwords = []
for row in MCC:
for x in list_of_words:
if x in row:
kwords.append(x)
It either gives me an empty bracket list again []
or it just creates a long list of the keywords repeating themselves.
What is the mistake am I making? Anyone can try to help me with the logic/solution.