Home > database >  python compare column one with word vs column two with label
python compare column one with word vs column two with label

Time:01-15

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 :-)

CodePudding user response:

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)
  • Related