【发布时间】:2015-04-13 23:36:11
【问题描述】:
我目前正在开发一个小型 anagram 程序,该程序会获取单词的所有可能排列并将它们与字典进行比较。但是,我无法打印结果。罪魁祸首似乎是 == 运算符,如果我输入 ''.join(words[i]) == compare[j] 则不会打印任何内容,但是,如果我输入 hi 并使用 ''.join(words[i]) == "hi" 运行程序,则会打印整个字典,但如果我将其反转为 "hi" == compare[j] 则不会打印任何内容。
提前感谢您的帮助!
import itertools
run = input("Please enter a word: ")
dictionary = "dictionary.txt" #input("Please enter the name of the dictionary file: ")
txt = open(dictionary)
compare = txt.readlines()
words = (list(itertools.permutations(run)))
for i in range(0, len(words)):
for j in range(0, len(compare)):
if ''.join(words[i]) == compare[j]:
print(compare[j])
【问题讨论】:
-
请注意,当你说你倒置它时,你没有。我建议您通过打印所有涉及的变量来进行调试。从您目前收集到的信息来看,问题似乎是
compare[j]。