S.J
S.J

Reputation: 119

Python: how to check if a string contains an element from a list and bring out that element?

I have a list of sentences (exg) and a list of fruit names (fruit_list). I have a code to check whether sentences contain elements from the fruit_list, as following:

exg = ["I love apple.", "there are lots of health benefits of apple.", 
       "apple is especially hight in Vitamin C,", "alos provide Vitamin A as a powerful antioxidant!"]


fruit_list = ["pear", "banana", "mongo", "blueberry", "kiwi", "apple", "orange"]

for j in range(0, len(exg)):
    sentence = exg[j]
    if any(word in sentence for word in fruit_list):
        print(sentence)

Output is below: only sentences contain words with "apple"

I love apple.
there are lots of health benefits of apple.
apple is especially hight in Vitamin C,

But I'd love to print out which word was an element of the fruit_list and was found in sentences. In this example, I'd love to have an output of the word "apple", instead of sentences contains the word apple.

Hope this makes sense. Please send help and thank you so so much!

Upvotes: 2

Views: 223

Answers (3)

Eventhisone
Eventhisone

Reputation: 86

Try using in to check for word in fruit_list, then you can use fruit as a variable later.

In order to isolate which word was found you'll need to use a different method than any(). any() only it only cares if it can find a word in fruit_list. it does not care which word or where in the list it was found.

exg = ["I love apple.", "there are lots of health benefits of apple.", 
   "apple is especially hight in Vitamin C,", "alos provide Vitamin A as a powerful antioxidant!"]


fruit_list = ["pear", "banana", "mongo", "blueberry", "kiwi", "apple", "orange"]

# You can remove the 0 from range, because it starts at 0 by default
# You can also loop through sentence directly
for sentence in exg:
    for word in fruit_list:
        if(word in sentence):
            print("Found word:", word, "  in:", sentence)

Result:

Found word: apple  in: I love apple.
Found word: apple  in: there are lots of health benefits of apple.
Found word: apple  in: apple is especially hight in Vitamin C,

Upvotes: 3

jpp
jpp

Reputation: 164653

Instead of any with a generator expression, you can use a for clause with break:

for j in range(0, len(exg)):
    sentence = exg[j]
    for word in fruit_list:
        if word in sentence:
            print(f'{word}: {sentence}')
            break

Result:

apple: I love apple.
apple: there are lots of health benefits of apple.
apple: apple is especially hight in Vitamin C,

More idiomatic is to iterate the list rather than a range for indexing:

for sentence in exg:
    for word in fruit_list:
        if word in sentence:
            print(f'{word}: {sentence}')
            break

Upvotes: 1

harish chava
harish chava

Reputation: 252

This will do the job.

for j in range(0, len(fruit_list)):
fruit = fruit_list[j]
if any(fruit in sentence for sentence in exg):
    print(fruit)

Upvotes: -2

Related Questions