[英]In Python: How can i get my code to print out all the possible words I can spell based on my input?
I think I was close to figuring out how to print out all the possible words based on user input from my set dictionary.我想我已经接近弄清楚如何根据我的词典中的用户输入打印出所有可能的单词。 it's based on the assumption that the user input is 'ART' so the possible words I have in my dictionary are ART, RAT, TART, and TAR but only the three letter combinations are printing out.
它基于用户输入是“ART”的假设,所以我的字典中可能的单词是 ART、RAT、TART 和 TAR,但只有三个字母组合被打印出来。 can anyone tell me where I am going wrong?
谁能告诉我哪里出错了? Thanks!
谢谢!
Dictionary = ["tar","art","tart","rat"] #creates dictionary of set words
StoredLetters = input('input your word here: ') #allows the user to input any word
list(StoredLetters)
def characters(word):
Dictionary = {}
for i in word:
Dictionary[i] = Dictionary.get(i, 0) + 1
return Dictionary
def all_words(StoredLetters, wordSet):
for word in StoredLetters:
flag = 1
words = characters(word)
for key in words:
if key not in wordSet:
flag = 0
else:
if wordSet.count(key) != words[key]:
flag = 0
if flag == 1:
print(word)
if __name__ == "__main__":
print(all_words(Dictionary, StoredLetters))
Based on the follow-up comments, the rule is that we must use all characters in the target word, but we can use each character as many times as we want.根据后续评论,规则是我们必须使用目标词中的所有字符,但我们可以根据需要多次使用每个字符。
I'd set up the lookup "dictionary" data structure as a Python dict
which maps sorted, unique characters as tuples in each dictionary word to a list of the actual words that can be formed from those characters.我将查找“字典”数据结构设置为 Python
dict
,它将每个字典单词中作为元组的排序的唯一字符映射到可以由这些字符形成的实际单词列表。
Next, I'd handle the lookups as follows:接下来,我将按如下方式处理查找:
set
means that we allow repetition and sorting the characters means we normalize for all of the possible permutations of those letters.set
意味着我们允许重复和排序字符意味着我们对这些字母的所有可能排列进行规范化。"artt"
correctly and prevent it from matching "art"
."artt"
这样的目标词,并防止它匹配"art"
。 Code:代码:
from collections import defaultdict
class Dictionary:
def __init__(self, words):
self.dictionary = defaultdict(list)
for word in words:
self.dictionary[tuple(sorted(set(word)))].append(word)
def search(self, target):
candidates = self.dictionary[tuple(sorted(set(target)))]
return [x for x in candidates if len(x) >= len(target)]
if __name__ == "__main__":
dictionary = Dictionary(["tar", "art", "tart", "rat"])
tests = ["art", "artt", "ar", "arttt", "aret"]
for test in tests:
print(f"{test}\t=> {dictionary.search(test)}")
Output: Output:
art => ['tar', 'art', 'tart', 'rat']
artt => ['tart']
ar => []
arttt => []
aret => []
The issues in the original code have been addressed nicely in the other answers.原始代码中的问题已在其他答案中得到很好的解决。 The logic doesn't seem clear since it's comparing characters to words and variable names often don't match the logic represented by the code.
逻辑似乎并不清晰,因为它将字符与单词进行比较,并且变量名称通常与代码表示的逻辑不匹配。
It's fine to use a frequency counter, but you'll be stuck iterating over the dictionary, and you'll need to check that each count of a character in a dictionary word is greater than the corresponding count in the target word.使用频率计数器很好,但是您将被困在对字典的迭代中,并且您需要检查字典单词中每个字符的计数是否大于目标单词中的相应计数。 I doubt the code I'm offering is optimal, but it should be much faster than the counter approach, I think.
我怀疑我提供的代码是最优的,但我认为它应该比计数器方法快得多。
It appears there are a few things that could contribute to this.似乎有一些事情可以促成这一点。
You are swapping the parameters on all words def allwords(Dictionary, StoredLetters):
when you call it in main allwords(StoredLetters, Dictionary)
.当您在 main
allwords(StoredLetters, Dictionary)
中调用它时,您正在交换所有单词def allwords(Dictionary, StoredLetters):
的参数。 Without specifying the name (look up named parameters in python) you would be swapping the input.如果不指定名称(在 python 中查找命名参数),您将交换输入。
In the characters
function it would appear you are resetting the dictionary variable.在
characters
function 中,您似乎正在重置字典变量。 Try using unique names when creating new variables.在创建新变量时尝试使用唯一名称。 This is causing the dictionary of words you set at the top to be emptied out when
characters(word)
is called这会导致您在顶部设置的单词字典在调用
characters(word)
时被清空
First off, you are confusing things by having the name of your variable StoredLetters
also being the name of one of the arguments to your all_words
function.首先,您的变量
StoredLetters
的名称也是您的all_words
function 的 arguments 之一的名称,这让您感到困惑。
Second, you are actually passing in StoredLetters
, which is art
, as the 2nd argument to the function, so it is wordSet
in the function, not StoredLetters
!其次,您实际上是传入
StoredLetters
,即art
,作为 function 的第二个参数,所以它是wordSet
中的 wordSet,而不是StoredLetters
!
You should really keep things more clear by using different variable names, and making it obvious what are you using for which argument.你真的应该通过使用不同的变量名来让事情更清楚,并让你清楚地知道你在使用哪个参数。
words
isn't really words, it's a dictionary with letters as keys, and how many times they appear as the values. words
不是真正的单词,它是一个以字母为键的字典,以及它们作为值出现的次数。 Making code clear and understandable goes a long way to making it easy to debug.使代码清晰易懂对易于调试大有帮助。 You have
word
, StoredLetters
, wordSet
, another StoredLetters
argument, words = characters(word)
which doesn't do what is expected.你有
word
, StoredLetters
, wordSet
,另一个StoredLetters
参数, words = characters(word)
没有做预期的事情。 This could all use a good cleanup.这都可以使用良好的清理。
As for the functionality, with art
, each letter only appears once, so for tart
, which has t
twice, if wordSet.count(key) != words[key]
will evaluate as True, and flag will be set to 0, and the word will not be printed.至于功能,对于
art
,每个字母只出现一次,所以对于tart
,它有t
两次, if wordSet.count(key) != words[key]
将评估为 True,并且 flag 将设置为 0,并且这个词不会被打印出来。
Hope that helps, and happy coding!希望对您有所帮助,并祝您编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.