python compare column one with word vs column two with label

Question:

I have two columns from two different csv. A column contains words. The other column contains sentences. I import the columns as a list. Now I want to find all sentences in which one of the imported words occurs. Then I want to write the sentence and the word back into a csv.

Does anyone have any idea how I need to design the comparison words vs sentences using Python?

Thanks in advance 🙂

Asked By: Georg Wildmoser

||

Answers:

On the basis of the two columns being separate Lists and you wanting a List to write back to a CSV then use something like:

l1= ['bill has a ball', 'tom has a toy', 'fred has a frog']

l2=['ball', 'frog']

sentences_with_words = []
for s in l1:
    for w in l2:
        if w in s:
            sentences_with_words.append([s, w])
  
print(sentences_with_words)
Answered By: user19077881
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.