Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove duplicate words in a string with Python?

Following example:

string1 = "calvin klein design dress calvin klein" 

How can I remove the second two duplicates "calvin" and "klein"?

The result should look like

string2 = "calvin klein design dress" 

only the second duplicates should be removed and the sequence of the words should not be changed!

like image 412
burner007 Avatar asked Oct 17 '11 13:10

burner007


People also ask

How do you remove duplicate text in Python?

We use file handling methods in python to remove duplicate lines in python text file or function. The text file or function has to be in the same directory as the python program file. Following code is one way of removing duplicates in a text file bar. txt and the output is stored in foo.

How do I remove repeating words from a string?

We create an empty hash table. Then split given string around spaces. For every word, we first check if it is in hash table or not. If not found in hash table, we print it and store in the hash table.


2 Answers

string1 = "calvin klein design dress calvin klein" words = string1.split() print (" ".join(sorted(set(words), key=words.index))) 

This sorts the set of all the (unique) words in your string by the word's index in the original list of words.

like image 145
Markus Avatar answered Oct 02 '22 14:10

Markus


def unique_list(l):     ulist = []     [ulist.append(x) for x in l if x not in ulist]     return ulist  a="calvin klein design dress calvin klein" a=' '.join(unique_list(a.split())) 
like image 31
spicavigo Avatar answered Oct 02 '22 14:10

spicavigo