0

I'm working on a "word generator" But I have a problem. Sometimes it creates unwanted words. I would like to remove them but not to heavy the loop.

I have to do like the example in the code with the whole alphabet. There would be nothing wrong with it that much, but there are a few other problems that I would have dealt with if only I knew how to cut it short.

# alph = "abcdefghijklmnopqrstuvwyzxą"

my_var = ["abc", "aabc", "cbd", "ccbd", "qwe", "qqwe"]
my_var2 = []


def removeDup():
    for x in my_var:
        if x.find("aa") == -1 and x.find("cc") == -1 and x.find("qq") == -1:
            my_var2.append(x)
    print(my_var2)
    
removeDup()

My idea is dynamic variables, but I can't make one loop in the other without creating chaos I tried something like the one in the picture, but I can only take out words with repeated letters enter image description here

Barmar
  • 741,623
  • 53
  • 500
  • 612
sectasy
  • 27
  • 5
  • 1
    How about different approach? You could just delete one of doubled char in string. Check out the link below. https://stackoverflow.com/questions/9841303/removing-duplicate-characters-from-a-string – Michał M Aug 02 '22 at 17:12

1 Answers1

2

There's no need for dynamic variables. Just make a list of all the duplicate characters.

dups = [z*2 for z in alph]
for x in open('xxx.txt', encoding='utf-8'):
    if not any(dup in x for dup in dups):
        print(x.strip())
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I am struggling with the automatic casting string-to-boolean of the _find_. It works because the repeated strings are always at the 1st place? – cards Aug 02 '22 at 17:19
  • 1
    Don't need to use `find()`, just use `x in y`. – Barmar Aug 02 '22 at 17:21
  • Thank you for your response! I like your idea and I think I'll solve my problem. Now unwanted words will not end up in my file. I admit at first for the test, I copied what you wrote and thought it didn't work. However, the argument is just missing when opening the file: D – sectasy Aug 02 '22 at 20:56