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.

+3


source to share


5 answers


IIUC your question, could you use

>>> [w for w in words if len(set(w)) == len(w)]
['cat', 'car', 'lizard']

      



If the word has no repeated letters, then its length will be the length of the set of its letters. The rest just puts it in the description.

+4


source


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

      

0


source


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']

      

0


source


In [1]: def duplicate (word): ...: for i in word: ...: if word.count (i)> 1: return True ...: return False

In [4]: ​​new = [word for word in words, if not duplicate (word)] In [5]: new Out [5]: ['cat', 'car', 'lizard']

0


source


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']

      

0


source







All Articles