[英]How to take a string and return a list of all the words in a dictionary that differ from this word by exactly one letter?
因此,现在我正在处理来自AZ的非常长的单词词典。 使用这个字典,我试图创建一个函数,该函数将字符串作为参数,并返回该字典中所有在任何时候都不同一个字母的单词。 例如。
>>> oneLetterDiff('find')
['bind', 'kind', 'lind', 'mind', 'rind', 'wind', 'fend', 'fond', 'fund', 'fine', 'fink', 'finn', 'fins']
>>> words=oneLetterDiff('hand')
>>> print words
['band', 'land', 'rand', 'sand', 'wand', 'hard', 'hang', 'hank', 'hans']
>>> oneLetterDiff('horse')
['morse', 'norse', 'worse', 'house', 'horde', 'horst']
>>> oneLetterDiff('monkey')
['donkey']
>>> oneLetterDiff('action')
[]
我已经导入了一个单独的函数,该函数在调用WordLookup的同时可以正常工作。 看起来像这样:
def createDictionary():
"""
Creates a global dict of all the words in the word file.
Every word from the word list file because a key in the dict.
Each word maps to the value None. This is because all we care about
is whether a given word is in the dict.
"""
global wordList # Specifies that wordList will not go away at the end
# of this function call and that other functions may
# use it
wordList = dict()
wordFile = open('WordList.txt')
for word in wordFile:
word = word.strip() # remove leading or trailing spaces
# map the word to an arbitrary value that doesn't take much
# space; we'll just be asking "in" questions of the dict
wordList[word] = None
wordFile.close()
def lookup(word):
global wordList # states that the function is using this global variable
return word in wordList
按照此代码,我有实际的oneLetterDiff函数:
def oneLetterDiff(myString):
theAlphabet = string.ascii_lowercase
for i in myString:
for j in theAlphabet:
#Maybe try to see if the letters can be changed in this fashion?
有谁能够帮助我更好地理解这一点? 我一直在努力寻找适当的解决方案,我们将为您提供任何帮助!
我想你不应该重新发明轮子。 有一个很好的python 库 ,可以实现Levenstein距离度量。 我认为您会发现它很有用。
让我们定义一个称为close_enough
的实用程序函数。 它需要两个单词,并且如果单词的长度相同且相差一个且只有一个字母,则返回True
:
def close_enough(word1, word2):
return len(word1) == len(word2) and 1 == sum(x!=y for x,y in zip(word1, word2))
接下来,我们需要一个功能通过单词列表,称为搜索wordlist
,并选择是的话close_enough
(由一个字母不同)。 这是执行此操作的功能。 它有两个参数:要比较的wordlist
myword
和wordlist
:
def one_letter_diff(myword, wordlist)
return [word for word in wordlist if close_enough(word, myword)]
如果您愿意,我们可以将wordlist
全局:
def one_letter_diff2(myword):
# Uses global wordlist
return [word for word in wordlist if close_enough(word, myword)]
但是,通常,如果避免使用全局变量,则程序逻辑更容易理解。
这是close_enough
动作,可以找出哪些单词相差一个字母,哪些没有:
In [22]: close_enough('hand', 'land')
Out[22]: True
In [23]: close_enough('hand', 'lend')
Out[23]: False
这是one_letter_diff
操作中的one_letter_diff
,用于查找wordlist
中与hand
相差一个字母的wordlist
:
In [26]: one_letter_diff('hand', ['land', 'melt', 'cat', 'hane'])
Out[26]: ['land', 'hane']
首先让我们看一下close_enough
。 如果满足两个条件,则返回True。 首先是单词的长度相同:
len(word1) == len(word2)
第二个区别是它们仅相差一个字母:
1 == sum(x!=y for x,y in zip(word1, word2))
让我们将其分解为几个部分。 对于每个不同的字母,它返回True:
[x!=y for x,y in zip(word1, word2)]
例如:
In [37]: [x!=y for x,y in zip('hand', 'land')]
Out[37]: [True, False, False, False]
sum
用于计算不同字母的数量。
In [38]: sum(x!=y for x,y in zip('hand', 'land'))
Out[38]: 1
如果该总和为1,则满足条件。
one_letter_diff
的命令是_list理解`:
[word for word in wordlist if close_enough(word, myword)]
仅当 close_enough
返回True时,它才会 close_enough
wordlist
每个wordlist
并将其包括在最终列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.