[英]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?
So right now I'm working with a very long dictionary of words from AZ. 因此,现在我正在处理来自AZ的非常长的单词词典。 With this dictionary I'm trying to create a function that takes a string as a parameter and returns all words within that dictionary that are one letter different at any point.
使用这个字典,我试图创建一个函数,该函数将字符串作为参数,并返回该字典中所有在任何时候都不同一个字母的单词。 Eg.
例如。
>>> 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')
[]
I've imported a separate function which is working perfectly at the moment I've called WordLookup. 我已经导入了一个单独的函数,该函数在调用WordLookup的同时可以正常工作。 It looks like this:
看起来像这样:
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
Following this code I have the actual oneLetterDiff function: 按照此代码,我有实际的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?
Is anyone able to help me understand this a bit better? 有谁能够帮助我更好地理解这一点? I've really been struggling to figure out a proper solution and any help is appreciated!
我一直在努力寻找适当的解决方案,我们将为您提供任何帮助!
I guess you shouldn't reinvent the wheel. 我想你不应该重新发明轮子。 There is a good python library that implements the Levenstein distance metric.
有一个很好的python 库 ,可以实现Levenstein距离度量。 I think you'll find it useful.
我认为您会发现它很有用。
Let's define a utility function called close_enough
. 让我们定义一个称为
close_enough
的实用程序函数。 It takes two words and returns True
if the words have the same length and differ by one and only one letter: 它需要两个单词,并且如果单词的长度相同且相差一个且只有一个字母,则返回
True
:
def close_enough(word1, word2):
return len(word1) == len(word2) and 1 == sum(x!=y for x,y in zip(word1, word2))
Next, we need a function to search through the word list, called wordlist
, and select the words that are close_enough
(differ by one letter). 接下来,我们需要一个功能通过单词列表,称为搜索
wordlist
,并选择是的话close_enough
(由一个字母不同)。 Here is a function to do that. 这是执行此操作的功能。 It takes two arguments: the word to compare against, called
myword
and the wordlist
: 它有两个参数:要比较的
wordlist
myword
和wordlist
:
def one_letter_diff(myword, wordlist)
return [word for word in wordlist if close_enough(word, myword)]
If you prefer, we could make wordlist
a global: 如果您愿意,我们可以将
wordlist
全局:
def one_letter_diff2(myword):
# Uses global wordlist
return [word for word in wordlist if close_enough(word, myword)]
Generally, though, program logic is easier to understand if globals are avoided. 但是,通常,如果避免使用全局变量,则程序逻辑更容易理解。
Here is close_enough
in action finding which words differ by one letter and which don't: 这是
close_enough
动作,可以找出哪些单词相差一个字母,哪些没有:
In [22]: close_enough('hand', 'land')
Out[22]: True
In [23]: close_enough('hand', 'lend')
Out[23]: False
Here is one_letter_diff
in action looking for words in wordlist
that differ by one letter from hand
: 这是
one_letter_diff
操作中的one_letter_diff
,用于查找wordlist
中与hand
相差一个字母的wordlist
:
In [26]: one_letter_diff('hand', ['land', 'melt', 'cat', 'hane'])
Out[26]: ['land', 'hane']
Let's look first at close_enough
. 首先让我们看一下
close_enough
。 It returns True if two conditions are satisfied. 如果满足两个条件,则返回True。 The first is that the words have the same length:
首先是单词的长度相同:
len(word1) == len(word2)
The second is that they differ by only one letter: 第二个区别是它们仅相差一个字母:
1 == sum(x!=y for x,y in zip(word1, word2))
Let's break that down into parts. 让我们将其分解为几个部分。 This returns True for every letter that differs:
对于每个不同的字母,它返回True:
[x!=y for x,y in zip(word1, word2)]
For example: 例如:
In [37]: [x!=y for x,y in zip('hand', 'land')]
Out[37]: [True, False, False, False]
sum
is used to count the number of letters that differ. sum
用于计算不同字母的数量。
In [38]: sum(x!=y for x,y in zip('hand', 'land'))
Out[38]: 1
If that sum is one, then the condition is satisfied. 如果该总和为1,则满足条件。
The command in one_letter_diff
is a _list comprehension`: one_letter_diff
的命令是_list理解`:
[word for word in wordlist if close_enough(word, myword)]
It goes through each word in wordlist
and includes it in the final list only if close_enough
returns True. 仅当
close_enough
返回True时,它才会 close_enough
wordlist
每个wordlist
并将其包括在最终列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.