消除基于字母的单词

时间:2016-04-30 02:44:33

标签: python

我有一本字典和一个字母:

import string
alphabet = list(string.ascii_lowercase)
dictionary = [line.rstrip('\n') for line in open("dictionary.txt")]

在一个函数中,我从字母表中删除了一个字母

alphabet.remove(letter)

现在,我想过滤字典以消除单词,如果它们包含不在字母表中的字母。

我试过循环:

for term in dictionary:
        for char in term:
            print term, char
            if char not in alphabet:
                dictionary.remove(term)
                break

然而,这会跳过某些词语。 我试过过滤器:

dictionary = filter(term for term in dictionary for char in term if char not in alphabet)

但我收到错误:

SyntaxError: Generator expression must be parenthesized if not sole argument

1 个答案:

答案 0 :(得分:4)

在迭代它时,您不想修改列表(或任何容器)。这可能会导致错误,似乎某些项目被跳过。如果您制作副本(dictionary[:]),它应该可以解决...

for term in dictionary[:]:
    for char in term:
        print term, char
        if char not in alphabet:
            dictionary.remove(term)
            break

我们在这里也可能做得更好......

alphabet_set = set(alphabet)  # set membership testing is faster than string/list...
new_dictionary = [
    term for term in dictionary
    if all(c in alphabet_set for c in term)]

另外,为dictionary实例避免名称list可能是明智的,因为dict实际上是内置类型... < / SUP>