-1

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

John
  • 11
  • 2
  • 1
    add sample input and expected output and also explain how actual output is different from the output you are expecting – deadshot Apr 29 '22 at 18:33
  • Actually I'm trying to remove duplicates but not remove duplicates of a specific word. Could you help me? – John Apr 29 '22 at 18:52
  • what is `wordsIter`? – WhiteAB Apr 29 '22 at 19:00
  • Why do you `break` when the `if` condition isn't satisfied? Any time `word==FIRST or word == LAST` your loop will break, so only the first instance of `FIRST` or `LAST` will ever be processed. Also you never add anything to your set in this situation. [How to debug small programs.](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](//stackoverflow.com/q/25385173/843953) – Pranav Hosangadi Apr 29 '22 at 19:03
  • Logically, `word!=FIRST or word!=LAST` is always true. See the linked duplicates. – kaya3 Apr 29 '22 at 19:44

1 Answers1

0

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']
physicalattraction
  • 6,485
  • 10
  • 63
  • 122