2

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:

  1. 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 the break line, to see when it was moving on to a new word).
  2. 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.
Auden Young
  • 1,147
  • 2
  • 18
  • 39
  • don't forget to close your open file, other wise you'll leak resources. Also see the with statement http://effbot.org/zone/python-with-statement.htm – reticentroot Apr 08 '17 at 02:36
  • You could check your reasoning by appending a word to a separate list if it does Not contain any of those letters. – wwii Apr 08 '17 at 03:31

1 Answers1

3

From the doc for statement.

If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy.

If I don't misunderstand your meaning, maybe you can try to make a shallow copy:

for n in words[:]:
    for i in n:
        if i in noletters:
            words.remove(n)
            break

Or maybe you can use this list comprehension:

words=[n for n in words if  not any([i in noletters for i in n])]

Hope this helps.

McGrady
  • 10,869
  • 13
  • 47
  • 69