Python regex remove duplicate words
I am very new a Python Show I want to change sentence if there are repeated words. Correct
Right now am I using this reg. but it do all so change on letters. Ex. "My friend and i is happy" --> "My friend and is happy" (it remove the "i" and space) ERROR
How can I do the same change but instead of letters it have to check on words? asked Jun 21, 2013 at 15:08
The answered Jun 21, 2013 at 15:15
tomtom 20.4k6 gold badges40 silver badges36 bronze badges Non- regex solution using
answered Jun 21, 2013 at 15:10
Ashwini ChaudharyAshwini Chaudhary 236k55 gold badges442 silver badges495 bronze badges 1
Result:
answered Feb 17, 2021 at 19:22
Not the answer you're looking for? Browse other questions tagged python regex or ask your own question.View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given a string str which represents a sentence, the task is to
remove the duplicate words from sentences using regular expression in java.
Approach
regex = "\\b(\\w+)(?:\\W+\\1\\b)+";
Below is the implementation of the above approach: C++
Java
Python3
Output: Good bye world Ram went to his home Hello world How do you delete repeated words in regex?Get the sentence. Form a regular expression to remove duplicate words from sentences.. The details of the above regular expression can be understood as: ... . Match the sentence with the Regex. ... . return the modified sentence.. How do you remove duplicates from a word in Python?1) Split input sentence separated by space into words. 2) So to get all those strings together first we will join each string in given list of strings. 3) Now create a dictionary using Counter method having strings as keys and their frequencies as values. 4) Join each words are unique to form single string.
How do you check for repeated words in Python?Python. string = "big black bug bit a big black dog on his big black nose";. #Converts the string into lowercase.. string = string.lower();. #Split the string into words using built-in function.. words = string.split(" ");. print("Duplicate words in a given string : ");. for i in range(0, len(words)):. count = 1;. |