I'm using Python 3.5 and my code is
words = open("wordlist.txt").readlines()
words = [k[:-2] for k in words]
noletters = ["b", "d", "f", "g", "h", "i", "j", "k", "l", "p", "q", "t", "y"]
for n in words:
for i in n:
if i in noletters:
words.remove(n)
break
words.sort()
words.sort(key = len)
print(words)
Here, wordlist.txt
is a really, really, really long list of words. First, I remove some unnecessary characters from each item in the list, and then I end up with a list where each item is a word - something like 'hello'
or yugoslavia
. This so far works.
Then, I create a list of letters that I don't want to be in any of the words that I get out of the program. Then for each item in the list, I check each letter in the word, and if the letter is in the list, I remove the word from the list and break out of the inner loop that checks for letters (this prevents the system from trying to delete something that's already been deleted). Then, I sort the list alphabetically and by the length of the word and the list is printed.
Problems:
- Nothing is being removed from the list by the loops, and I have no idea why. It's getting into the loops for sure - I added a few print statements (after the
words.remove(n)
line to print when something was removed, and also after thebreak
line, to see when it was moving on to a new word). - The statement that sorts by length of strings also adds a bunch of stuff that was never in the list of words - for example,
'o'
wasn't in the list, but it was added, along with a couple of blank strings (''
) and so on - it looked like all the possible combinations of each letter of the alphabet.