EllisR
EllisR

Reputation: 11

Python make a list of words from a file

I'm trying to make a list of words from a file that includes only words that do not contain any duplicate letters such as 'hello' but 'helo' would be included.

My code words perfectly when I use a list that I create by just typing in words however when I try to do it with the file list it just prints all the words even if they include duplicate letters.

words = []
length = 5
file = open('dictionary.txt')
for word in file:
    if len(word) == length+1:
        words.insert(-1, word.rstrip('\n'))
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
x = 0
while x in range(0, len(alpha)):
    i = 0
    while i in range(0, len(words)):
        if words[i].count(alpha[x]) > 1:
            del(words[i])
            i = i - 1
        else:
            i = i + 1
    x = x + 1
print(words)

Upvotes: 1

Views: 1698

Answers (2)

Ryan Collingham
Ryan Collingham

Reputation: 99

What does your dictionary.txt look like? Your code should work so long as each word is on a separate line (for x in file iterates through lines) and at least some of the words have 5 non-repeating letters.

Also, couple of tips:

  • You can read lines from a file into a list by calling file.readlines()
  • You can check for repeats in a list or string by using sets. Sets remove all duplicate elements, so checking if len(word) == len(set(word)) will tell you if there are duplicate letters in much less code :)

Upvotes: 1

OptimusCrime
OptimusCrime

Reputation: 14863

This snippet adds words, and removes duplicated letters before inserting them

words = []
length = 5
file = open('dictionary.txt')
for word in file:
    clean_word = word.strip('\n')
    if len(clean_word) == length + 1:
        words.append(''.join(set(clean_word))

We convert the string to a set, which removed duplicates, and then we join the set to a string again:

>>> word = "helloool"
>>> set(word)
set(['h', 'e', 'l', 'o'])
>>> ''.join(set(word))
'helo'

I am not 100% sure how you want to remove duplicates like this, so I've assumed no letter can be more than once in the word (as your question specifies "duplicate letter" and not "double letter").

Upvotes: 1

Related Questions