我目前正在研究一个小的anagram程序,它可以对一个单词进行所有可能的排列,并将它们与字典进行比较。但是,我无法将结果打印出来。罪魁祸首似乎是==运算符,如果我把''.join(words[i]) == compare[j]
没有打印,但是,如果我输入hi并用''.join(words[i]) == "hi"
运行程序整个字典打印,但如果我将其反转为{{没有打印。
提前感谢您的帮助!
"hi" == compare[j]
答案 0 :(得分:3)
compare = txt.readlines()
readlines()
不会从每一行中删除行结尾,因此每行最后会有一个\n
。这会导致您与compare[j]
的所有比较失败。
您可以使用类似的内容删除\n
。
compare = [line.strip() for line in txt]
答案 1 :(得分:2)
请注意,如果您的字词包含W
个字母,并且您的字词包含D
个字词,则您的搜索正在进行W! * D
次比较。
您可以通过将这两个单词转换为规范形式(即按字母顺序排列的字母)将此缩小为D
比较。
如果您要搜索N
个字词,可以将字词存储为D / N
,将其逐字减少到{canonical_form: [list,of,matching,words]}
每个字词的比较(摊销):
from collections import defaultdict
DICT_FILE = "dictionary.txt"
def canonize(word):
# "hello\n" => "ehllo"
return "".join(sorted(word.strip()))
def load_dict(fname=DICT_FILE):
lookup = defaultdict(list)
with open(fname) as inf:
for line in inf:
word = line.strip()
canon = canonize(word)
lookup[canon].append(word)
# lookup["ehllo"] = ["hello"]
return lookup
def main():
anagrams = load_dict()
while True:
word = input("Enter word to search for (or hit Enter to quit): ").strip()
if not word:
break
else:
canon = canonize(word)
if canon in anagrams:
print("Found: " + ", ".join(anagrams[canon]))
else:
print("No anagrams found.")
if __name__ == "__main__":
main()
然后像
一样运行Enter word to search for (or hit Enter to quit): tester
Found: retest, setter, street, tester
Enter word to search for (or hit Enter to quit): binary
Found: binary, brainy
Enter word to search for (or hit Enter to quit): ttt
No anagrams found.
Enter word to search for (or hit Enter to quit):
答案 2 :(得分:-1)
从变量中替换换行符:
compare = compare.replace('\n', '')