Python 有没有办法检查生成列表中的单词是否在文本文件中?
我想检查生成列表中的单词是否在文本文件中 我制作了一个字谜生成器,我想通过检查该生成器创建的列表是否在英语词典中,看看它是否有一个真实的单词Python 有没有办法检查生成列表中的单词是否在文本文件中?,python,python-3.x,Python,Python 3.x,我想检查生成列表中的单词是否在文本文件中 我制作了一个字谜生成器,我想通过检查该生成器创建的列表是否在英语词典中,看看它是否有一个真实的单词 #input word = input("Word: ") #function to generate anagrams of the word def make_anagram(word): if len(word) <= 1: yield word else: for let
#input
word = input("Word: ")
#function to generate anagrams of the word
def make_anagram(word):
if len(word) <= 1:
yield word
else:
for let in make_anagram(word[1:]):
for i in range(len(word)):
yield let[:i] + word[0:1] + let[i:]
#function to check anagrams
def check_if_anagram(word):
#this is the file with the dictionary
file = open("english-words-master\words.txt")
words = file.read()
#here's where I'm havning trouble
anagram = list(make_anagram(word))
if str(anagram) in words and str(anagram) != word:
print(str(anagram) + " is a real anagram.")
else:
print("there is no real anagram for " + word)
file.close()
#输入
字=输入(“字:”)
#函数生成单词的字谜
def make_字谜(word):
如果len(word)您正在检查整个列表是否在文件中。例如,如果anagram=['abc','acb']
在这里,您实际上是在检查:
if "['abc', 'acb']" in words
这很可能永远不会发生
您要做的是检查列表中的每个字谜:
anagrams=列表(生成anagram(word))
对于字谜中的字谜:
如果字谜是单词和字谜!=字:
打印(字谜+“是真正的字谜。”)
打破
其他:
打印(“没有“+字”的真正的字谜)