解析Python中嵌套在字典中具有相似字符的单词的列表

发布于 2025-01-18 20:33:31 字数 602 浏览 1 评论 0原文

作为我的第一个个人项目,我正在构建一个带有第一个猜测单词的单词“求解器”,然后将单词中正确的字母输入,然后在单词列表中搜索具有相似字符的单词。为此,我将每个单词存储在字典中,关键在于单词,值是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2025-01-25 20:33:31

您可能正在寻找比较整个列表,而不是letters

correct_letters_1 = list(input("Enter correct letters: ").strip())

# correct_letters_1 is a whole list
if correct_letters_1 in word_dictionary.values():
    print("yes")

# Inputting "abc"
>>> correct_letters_1
['a', 'b', 'c']
>>> word_dictionary
{'abc': ['a', 'b', 'c']}
>>> correct_letters_1 in word_dictionary.values()
True

检查用户的猜测是否为a subset 任何单词:

correct_letters_1 = list(input("Enter correct letters: ").strip())
let_set = set(correct_letters_1)

word_dictionary = {'abc': ['a', 'b', 'c'],
                   'abcde': ['a', 'b', 'c', 'd', 'e'],
                   'ab': ['a', 'b'],
                   'test': ['t', 'e', 's', 't']}

# Check to see if correct_letters_1 is a subset of any word in the dictionary
close_words = ["".join(x) for x in word_dictionary.values() if let_set <= set(x)]

可以简化为:

correct_letters_1 = input("Enter correct letters: ").strip()
let_set = set(correct_letters_1)

words = ["abc", "test", "ab"]

# Check to see if correct_letters_1 is a subset of any word in the list words
close_words = [x for x in words if let_set <= set(x)]

You might be looking to compare the whole list, rather than the letters:

correct_letters_1 = list(input("Enter correct letters: ").strip())

# correct_letters_1 is a whole list
if correct_letters_1 in word_dictionary.values():
    print("yes")

# Inputting "abc"
>>> correct_letters_1
['a', 'b', 'c']
>>> word_dictionary
{'abc': ['a', 'b', 'c']}
>>> correct_letters_1 in word_dictionary.values()
True

To check if the user's guess is a subset of any of the words:

correct_letters_1 = list(input("Enter correct letters: ").strip())
let_set = set(correct_letters_1)

word_dictionary = {'abc': ['a', 'b', 'c'],
                   'abcde': ['a', 'b', 'c', 'd', 'e'],
                   'ab': ['a', 'b'],
                   'test': ['t', 'e', 's', 't']}

# Check to see if correct_letters_1 is a subset of any word in the dictionary
close_words = ["".join(x) for x in word_dictionary.values() if let_set <= set(x)]

Which can be simplified to:

correct_letters_1 = input("Enter correct letters: ").strip()
let_set = set(correct_letters_1)

words = ["abc", "test", "ab"]

# Check to see if correct_letters_1 is a subset of any word in the list words
close_words = [x for x in words if let_set <= set(x)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文