0

Im trying to delete all words from a list that include duplicate letters (to start with 'd' and 'r' for testing) but cannot get it check all the letters i want it to.

a = ["word","worrd","worrrd","wordd"]
alpha = ['d','r']
i = 0
x = 0
while x in range(0, len(alpha)):
    while i in range(0, len(a)):
        if a[i].count(alpha[x]) > 1:
            del(a[i])
            i = i - 1
        else:
            i = i + 1
    x = x + 1
print(a)
  • you are not resetting i in "x" loop therefore, this cause issues in matching for next character. – mangupt May 13 '17 at 09:03

4 Answers4

2

Short solution using re.search() function:

import re

a = ["word","worrd","worrrd","wordd"]
result = [w for w in a if not re.search(r'([dr]).*\1', w)]

print(result)

The output:

[word]

([dr]).*\1 - regex pattern to check if some letter from character class [dr] occurs at least twice within a word

.* - matches 0 or more characters

\1 - points to the first captured group (...)


Another short alternative is using built-in all() function:

a = ["word","worrd","worrrd","wordd"]
alpha = ['d','r']
result = [w for w in a if all(w.count(c) == 1 for c in alpha)]

print(result)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

This is your method only except I just added i = 0 after first while loop

a = ["word","worrd","worrrd","wordd"]
alpha = ['d','r']
i = 0
x = 0
while x in range(0, len(alpha)):
    i = 0
    while i in range(0, len(a)):
        if a[i].count(alpha[x]) > 1:
            del(a[i])
            i = i - 1
        else:
            i = i + 1
    x = x + 1
print(a)

['word']
Shashank
  • 1,105
  • 1
  • 22
  • 35
0

you doing it right but you forgot to reset the value of "i" in while loop of x

a = ["word","worrd","worrrd","wordd"]
alpha = ['d','r']
x = 0
while x in range(0, len(alpha)):
    # i should be reset here
    i = 0
    while i in range(0, len(a)):
        if a[i].count(alpha[x]) > 1:
            del(a[i])
            i = i - 1
        else:
            i = i + 1
    x = x + 1
print(a)
mangupt
  • 369
  • 2
  • 11
0

What you likely are after, is when you have two d:s or r:s in a row. Then you want a regex as:

import re

a = ["word","worrd","worrrd","wordd", "worded"]
result = [w for w in a if not re.search(r'(dd)|(rr)', w)

print(result)
JohanL
  • 6,671
  • 1
  • 12
  • 26