How to not double print when matching multiple words in a sentence

Question:

Say we have the following sentence and words to match

sentences = ['There are three apples and oranges in the fridge.', 'I forgot the milk.']
wordsMatch = ['apples', 'bananas', 'fridge']

How to iterate through the sentences and print them if there is a match without double / triple printing it if it finds more than 1 matches?

For instance, the following code will print the first sentence twice because it finds apples and fridge:

matchedSentences = [sentence for sentence in sentences for word in wordsMatch if word in sentence]

# output: ['There are three apples and oranges in the fridge.', 'There are three apples and oranges in the fridge.']

What solution would be most appropriate?

Asked By: DarknessPlusPlus

||

Answers:

output = [
    sentence 
    for sentence in sentences 
    if any(word in sentence for word in wordsMatch)
]

The any function returns True as soon as one of the elements in the iterable provided is True. Therefore we can simply loop over the sentences and check that any of the words is in a sentence.

PS:

I am assuming that the solution should be case-sensitive. If not, you can adjust the word in sentence-check accordingly:

output = [
    sentence
    for sentence in sentences
    if any(word.lower() in sentence.lower() for word in wordsMatch)
]

PPS: (just for fun)

Here is how to do it in-place with the sentences list:

removed = 0
for i, sentence in enumerate(reversed(sentences), start=1):
    if not any(word in sentence for word in wordsMatch):
        del sentences[-i + removed]
        removed += 1
Answered By: Daniil Fajnberg
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.