I’m trying to remove all the duplicate words except for two certain words.
def reduceDuplicates(words, wordsIter):
theWords=set()
for word in words:
if word!=FIRST or word!=LAST: #FIRST="first" #LAST="last"
if word not in theWords:
theWords.add(word) #create new txt file
else:
break
words.__init__(theWords) #call constructor to print
Does anybody know where to go with this? I’ve tried everything I can think of. The words “first” and “last” in my txt file keep losing their duplicates. I’m trying to keep those duplicates.
wordsIter is a list iterator that is supposed to handle mutator and accessor methods on the list of words variable
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It is a bit unclear what words and wordsIter are in your code example. This function should do the trick though:
from typing import List, Set
def remove_duplicates(words: List[str], words_with_duplicates: Set[str]) -> List[str]:
"""
Return the list of words in the same order, with all duplicates except those from `words_with_duplicates` removed
"""
seen = set()
return [x for x in words if x in words_with_duplicates or not (x in seen or seen.add(x))]
output = remove_duplicates(words=['A', 'B', 'C', 'B', 'D', 'D', 'C', 'B', 'D', 'B', 'A'],
words_with_duplicates={'B', 'D'})
assert output == ['A', 'B', 'C', 'B', 'D', 'D', 'B', 'D', 'B']
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0