Remove duplicate words from string python
Following example: Show
How can I remove the second two duplicates The result should look like
only the second duplicates should be removed and the sequence of the words should not be changed!
Martin Thoma 112k147 gold badges567 silver badges873 bronze badges asked Oct 17, 2011 at 13:08
This sorts the set of all the (unique) words in your string by the word's index in the original list of words. answered Oct 17, 2011 at 13:40
MarkusMarkus 3,3073 gold badges23 silver badges25 bronze badges
answered Oct 17, 2011 at 13:12
spicavigospicavigo 3,98020 silver badges28 bronze badges 7 In Python 2.7+, you could use
answered Oct 17, 2011 at 13:21
NPENPE 470k103 gold badges921 silver badges994 bronze badges 1 Cut and paste from the itertools recipes
I really wish they could go ahead and make a module out of those recipes soon. I'd very much like to be able to do Use like this:
answered Oct 17, 2011 at 13:22
3
Explanation:
answered Nov 9, 2018 at 8:33
3
answered Oct 17, 2011 at 13:27
ekhumoroekhumoro 110k19 gold badges215 silver badges321 bronze badges You can use a set to keep track of already processed words.
answered Oct 17, 2011 at 13:10
Pablo Santa CruzPablo Santa Cruz 172k31 gold badges236 silver badges289 bronze badges 2 Several answers are pretty close to this but haven't quite ended up where I did:
Of course, if you want it a tiny bit cleaner or faster, we can refactor a bit:
I think the second version is about as performant as you can get in a small amount of code. (More code could be used to do all the work in a single scan across the input string but for most workloads, this should be sufficient.) answered Oct 17, 2011 at 22:13
Chris PhillipsChris Phillips 10.7k3 gold badges33 silver badges45 bronze badges Use numpy function make an import its better to have an alias for the import (as np)
and then you can bing it like this for removing duplicates from array you can use it this way
for your case if you want result in string you can use
answered Jun 8, 2020 at 12:04
Sulman MalikSulman Malik 1351 gold badge1 silver badge7 bronze badges 11 and 2 work perfectly:
and 2
answered Apr 17, 2016 at 16:38
1 Question: Remove the duplicates in a string
answered Jun 16, 2018 at 23:44
You can remove duplicate or repeated words from a text file or string using following codes -
P.S. -Do identations as per required. Hope this helps!!! answered Jun 25, 2018 at 7:22
Without using the split function (will help in interviews)
Taazar 1,54517 silver badges27 bronze badges answered Mar 6, 2020 at 12:06
initializing list
using |