【发布时间】:2017-05-15 00:59:45
【问题描述】:
我正在尝试从一个文件中创建一个单词列表,该文件仅包含不包含任何重复字母(例如“hello”但会包含“helo”)的单词。
当我使用仅通过输入单词创建的列表时,我的代码词完美,但是当我尝试使用文件列表执行此操作时,它只会打印所有单词,即使它们包含重复的字母。
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)
【问题讨论】:
-
def has_double_letter(word): return len(word) != len(set(word))怎么样? -
@KlausD。如果可能,我想保留当前方法以使其简单,有什么想法吗??
-
然后给出比“不起作用”更好的错误描述。
-
@KlausD。当我使用我输入的单词列表运行它时,它可以完美运行。但是,当我以这种方式使用文件运行它时,它仍然包含所有单词,即使它们包含重复的字母
-
现在通过编辑将错误描述添加到问题中。