Python - Check if given words appear together in a list of sentence


Say we have a list containing small sentences as its elements. We have another list which contains some of the words used in this sentences of the first list. We want to find out if two words from the second list are present together in some of the sentences of the first list or not.

With append and for loop

We use the for loop with in condition to check for the presence of words in the lists of sentences. Then we use the len function to check if we have reached the end of the list.

Example

 Live Demo

list_sen = ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
list_wrd = ['Eggs', 'Fruits']

print("Given list of sentences: \n",list_sen)
print("Given list of words: \n",list_wrd)

res = []
for x in list_sen:
   k = [w for w in list_wrd if w in x]
   if (len(k) == len(list_wrd)):
      res.append(x)
print(res)

Output

Running the above code gives us the following result −

Given list of sentences:
['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
Given list of words:
['Eggs', 'Fruits']
['Eggs and Fruits on Wednesday']

With all

Here we design a for loop for checking if the words are present in the list containing sentences and then apply all function to verify indeed all the words are present in the sentence.

Example

 Live Demo

list_sen = ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
list_wrd = ['Eggs', 'Fruits']

print("Given list of sentences: \n",list_sen)
print("Given list of words: \n",list_wrd)

res = [all([k in s for k in list_wrd]) for s in list_sen]
print("\nThe sentence containing the words:")
print([list_sen[i] for i in range(0, len(res)) if res[i]])

Running the above code gives us the following result −

Output

Given list of sentences:
['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
Given list of words:
['Eggs', 'Fruits']

The sentence containing the words:
['Eggs and Fruits on Wednesday']

With lambda and map

We can take a similar approach as above but with lambda and map functions. We also use the split functions and check the availability of the all the given words in the list with sentences. The map function is used to apply this logic again and to each element of the lists.

Example

 Live Demo

list_sen = ['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
list_wrd = ['Eggs', 'Fruits']

print("Given list of sentences: \n",list_sen)
print("Given list of words: \n",list_wrd)

res = list(map(lambda i: all(map(lambda j:j in i.split(),
list_wrd)), list_sen))

print("\nThe sentence containing the words:")
print([list_sen[i] for i in range(0, len(res)) if res[i]])

Output

Running the above code gives us the following result −

Given list of sentences:
['Eggs on Sunday', 'Fruits on Monday', 'Eggs and Fruits on Wednesday']
Given list of words:
['Eggs', 'Fruits']

The sentence containing the words:
['Eggs and Fruits on Wednesday']

Updated on: 10-Jul-2020

905 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements