解析Python中嵌套在字典中具有相似字符的单词的列表
作为我的第一个个人项目,我正在构建一个带有第一个猜测单词的单词“求解器”,然后将单词中正确的字母输入,然后在单词列表中搜索具有相似字符的单词。为此,我将每个单词存储在字典中,关键在于单词,值是IE Dodge一词中的每个字符:[D,O,D,G,E]。我目前正在实验以通过字典的值进行解析的代码运行,以使字符与输入的字母匹配,但在如何浏览字典中的列表中作为循环而感到困惑。
这是我输入正确字母的代码:
correct_letters_1 = list(letters for letters in input("Input corrects letters: ").strip().split())
这是我解析词典的尝试之一:
for letters in correct_letters_1:
if letters in word_dictionary.values():
print("yes")
我敢肯定,这个问题与我的代码与词典中的整个列表有关,而不是每个单独的字符都有关系,即使我尝试尝试输入整个单词,即输入正确的字母:道奇;我仍然没有任何输出。
如果有人可以将初学者放在正确的轨道上,那将不胜感激。
As my first personal project I am building a Wordle "solver" that comes up with a first guess word then takes an input of the letters that were correct in the word then searches the word list for words with similar characters. To do this I stored each word in a dictionary with the key being the word and the value being each character in the word i.e dodge: [d, o, d, g, e]. I am currently experimenting to get code running that parses through the values of the dictionary to match the characters to the inputted letters but am stumped on how to search through the lists within the dictionary as a loop.
Here's my code for inputting the correct letters:
correct_letters_1 = list(letters for letters in input("Input corrects letters: ").strip().split())
Here's one of my attempts at parsing the dictionary:
for letters in correct_letters_1:
if letters in word_dictionary.values():
print("yes")
I'm pretty sure this problem has something to do with my code parsing the entire lists within the dictionary rather than each individual character but even when I try inputting the whole word, i.e Input Correct Letters: d o d g e; I still don't have any output.
If someone could put a beginner on the right track it would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在寻找比较整个列表,而不是
letters
:检查用户的猜测是否为a subset 任何单词:
可以简化为:
You might be looking to compare the whole list, rather than the
letters
:To check if the user's guess is a subset of any of the words:
Which can be simplified to: