- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
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
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']
- Related Articles
- Check if all occurrences of a character appear together in Python
- Check if a list exists in given list of lists in Python
- Count words in a sentence in Python program
- Python program to count words in a sentence
- Python - Generate all possible permutations of words in a Sentence
- Program to check a string can be broken into given list of words or not in python
- Check if a two-character string can be made using given words in Python
- Python program to sort Palindrome Words in a Sentence
- Find k longest words in given list in Python
- C# program to remove all duplicates words from a given sentence
- Java program to remove all duplicates words from a given sentence
- Python Check if suffix matches with any string in given list?
- Rearrange Words in a Sentence in C++
- Counting number of words in a sentence in JavaScript
- Python - Check if a list is contained in another list
