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!
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.
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.
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.
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()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With