How to create a list of all words in a text file except for words with duplicate letters
I am trying to iterate over a list and add all words to a new list, excluding words with duplicate letters.
Example:
words = ['cat', 'car', 'weevil', 'lizard', 'mammoth', 'cabbage', 'aardvaark']
newlist = []
for word in words:
for letter in word
if word.count(letter) > 1:
pass
else:
newlist.append(word)
print(newlist)
# Result
['cat', 'cat', 'cat', 'car', 'car', 'car', 'weevil', 'weevil', 'weevil', 'weevil', 'lizard', 'lizard', 'lizard', 'lizard', 'lizard', 'lizard', 'mammoth', 'mammoth', 'mammoth', 'mammoth', 'cabbage', 'cabbage', 'cabbage', 'aardvaark', 'aardvaark', 'aardvaark']
The code doesn't really work because a word like weevil, for example, satisfies word.count (letter) for the letter "w" is passed twice for two "e" and then satisfies three more times after two 'e's, so it appears listed four times.
I may be approaching this from the wrong angle, but I really don't understand how to do this.
source to share
Try using the filter
functionality provided by the standard library
words = ['cat', 'car', 'weevil', 'lizard', 'mammoth', 'cabbage', 'aardvaark']
newlist = list( filter(lambda x: unduplicate_letter(x), words) )
def unduplicate_letters(w):
for letter in w:
if w.count(letter) > 1:
return False
return True
source to share
This is because he adds a word every time he checks the letter. You have to check all letters before adding the word, so you can add a check variable to see if all letters are being transmitted and then add the word. eg:
words = ['cat', 'car', 'weevil', 'lizard', 'mammoth', 'cabbage', 'aardvaark']
newlist = []
for word in words:
check = 0
for letter in word:
if word.count(letter) > 1:
check = 1
if (check == 0):
newlist.append(word)
print(newlist)
OUTPUT:
['cat', 'car', 'lizard']
source to share
We can use Python set()
to help with a simpler solution:
$ cat /tmp/tmp.py
from __future__ import print_function
words = ['cat', 'car', 'weevil', 'lizard', 'mammoth', 'cabbage', 'aardvaark']
newlist = list()
for word in words:
# if word inlude multiple occurances of same character, set()
# will keep only one occurance.
if len(set(word)) == len(word):
newlist.append(word)
print(newlist)
$ python /tmp/tmp.py
['cat', 'car', 'lizard']
source to share