Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python program to remove words that are common in two Strings
When working with text processing, you may need to remove words that are common between two strings and keep only the unique words. This can be achieved by counting word frequencies and filtering words that appear only once across both strings.
Example
Below is a demonstration of removing common words from two strings ?
def common_words_filter(string_1, string_2):
word_count = {}
# Count words from first string
for word in string_1.split():
word_count[word] = word_count.get(word, 0) + 1
# Count words from second string
for word in string_2.split():
word_count[word] = word_count.get(word, 0) + 1
# Return words that appear only once (uncommon words)
return [word for word in word_count if word_count[word] == 1]
string_1 = "Python is fun"
print("The first string is:")
print(string_1)
string_2 = "Python is fun to learn"
print("The second string is:")
print(string_2)
print("The uncommon words from the two strings are:")
print(common_words_filter(string_1, 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']
Using Set Operations
An alternative approach using set operations for better readability ?
def remove_common_words_set(string_1, string_2):
words_1 = set(string_1.split())
words_2 = set(string_2.split())
# Find words unique to each string
unique_words = words_1.symmetric_difference(words_2)
return list(unique_words)
string_1 = "Python is fun"
string_2 = "Python is fun to learn"
print("Unique words using set operations:")
print(remove_common_words_set(string_1, string_2))
Unique words using set operations: ['learn', 'to']
How It Works
The first approach uses a dictionary to count word frequencies:
- Word counting: Each word from both strings is counted in a dictionary
- Filtering: Words with count = 1 are unique to one string only
- List comprehension: Creates the final list of uncommon words
The second approach uses set operations:
- Set creation: Converts word lists to sets for faster operations
- Symmetric difference: Finds words present in either set but not both
Conclusion
Use dictionary counting for frequency-based filtering or set operations for simpler unique word extraction. Both methods effectively remove common words between two strings.
Advertisements
