== 与 readlines() 中的行进行比较失败
== comparisons against lines from readlines() fail
我目前正在开发一个小的字谜程序,它获取一个词的所有可能排列并将它们与字典进行比较。但是,我无法打印结果。罪魁祸首似乎是 == 运算符,如果我输入 ''.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 = txt.readlines()
readlines()
不会去除每行的行尾,因此每行的末尾都会有一个 \n
。这会导致您与 compare[j]
的所有比较都失败。
你可以用类似的东西删除 \n
。
compare = [line.strip() for line in txt]
替换变量中的换行符:
compare = compare.replace('\n', '')
请注意,如果您的单词有 W
个字母,并且您的词典有 D
个单词,则您的搜索正在进行 W! * D
比较。
您可以将这两个词都转换为规范形式(即按字母顺序排列的字母),从而将其简化为 D
比较。
如果您要搜索 N
个词,您可以通过将字典存储为 {canonical_form: [list,of,matching,words]}
:[=19= 进一步减少每个词的 D / N
次比较(摊销) ]
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):
我目前正在开发一个小的字谜程序,它获取一个词的所有可能排列并将它们与字典进行比较。但是,我无法打印结果。罪魁祸首似乎是 == 运算符,如果我输入 ''.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 = txt.readlines()
readlines()
不会去除每行的行尾,因此每行的末尾都会有一个 \n
。这会导致您与 compare[j]
的所有比较都失败。
你可以用类似的东西删除 \n
。
compare = [line.strip() for line in txt]
替换变量中的换行符:
compare = compare.replace('\n', '')
请注意,如果您的单词有 W
个字母,并且您的词典有 D
个单词,则您的搜索正在进行 W! * D
比较。
您可以将这两个词都转换为规范形式(即按字母顺序排列的字母),从而将其简化为 D
比较。
如果您要搜索 N
个词,您可以通过将字典存储为 {canonical_form: [list,of,matching,words]}
:[=19= 进一步减少每个词的 D / N
次比较(摊销) ]
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):