- 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 program to remove words that are common in two Strings
When it is required to remove words that are common in both the strings, a method is defined that takes two strings. The strings are spit based on spaces and list comprehension is used to filter out the result.
Example
Below is a demonstration of the same
def common_words_filter(my_string_1, my_string_2): my_word_count = {} for word in my_string_1.split(): my_word_count[word] = my_word_count.get(word, 0) + 1 for word in my_string_2.split(): my_word_count[word] = my_word_count.get(word, 0) + 1 return [word for word in my_word_count if my_word_count[word] == 1] my_string_1 = "Python is fun" print("The first string is :") print(my_string_1) my_string_2 = "Python is fun to learn" print("The second string is :") print(my_string_2) print("The result is :") print(common_words_filter(my_string_1, my_string_2))
Output
The first string is : Python is fun The second string is : Python is fun to learn The uncommon words from the two strings are : ['to', 'learn']
Explanation
A method named ‘common_words_filter’ is defined that takes two strings as parameters.
An empty dictionary is defined,
The first string is split based on spaces and iterated over.
The ‘get’ method is used to get the word and the specific index.
The same is done for the second string.
The list comprehension is used to iterate through the dictionary and check if the word count is 1 or not.
Outside the method, two strings are defined and are displayed on the console.
The method is called by passing the required parameter.
The output is displayed on the console.
- Related Articles
- Common Words in Two Strings in Python
- Common words among tuple strings in Python
- Python program to find uncommon words from two Strings
- Function to check two strings and return common words in JavaScript
- Program to find size of common special substrings of two given strings in Python
- Program to determine if two strings are close in Python
- Python Program that Displays which Letters are in the Two Strings but not in Both
- Program to partition two strings such that each partition forms anagram in Python
- Python code to print common characters of two Strings in alphabetical order
- Program to find longest common prefix from list of strings in Python
- Remove space between two words in MySQL?
- Program to find length of longest common subsequence of three strings in Python
- How to extract the strings between two words in R?
- Program to merge two strings in alternating fashion in Python
- How to find the longest common substring from more than two strings in Python?
