简体   繁体   English

如何获取字符串并返回字典中与该单词相差一个字母的所有单词的列表?

[英]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 mywordwordlist

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. 但是,通常,如果避免使用全局变量,则程序逻辑更容易理解。

Examples 例子

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']

How it works 这个怎么运作

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.

相关问题 查找列表中仅相差一个字母的所有单词 - Find all the words in the list that differ by a single letter 仅当另一个单词中第一个单词的索引都匹配时,如何返回一个单词与列表中所有其他单词的索引的匹配? - How match index of one word to index of all other words in list return only if all index match of first word in other any word? 用字典中的单词替换字符串的每个字母 - Replace each letter of a string with words from a dictionary 如何验证python中来自单词的字母索引是否匹配来自列表中单词的字母索引? - How to verify if indexes of letter from word matches index of letter from words from list in python? 查找字符串中仅包含一个字母的单词 - Finding words that contain all but one letter in string 从先前的单词词典创建一个包含所有字母过渡的词典? - Create a dictionary of all letter transitions from a previous dictionary of words? 如何从以相同字母开头和结尾的单词创建单词部分的字典 - How to create a dictionary of word parts from the words that start and end with the same letter 将单词列表中的所有单词替换为python中的另一个单词 - Replace all words from word list with another string in python 从列表中的一个字符串中搜索任何单词或单词组合(python) - Search for any word or combination of words from one string in a list (python) 如何检查一个单词是否包含另一个字符串中的一个字母 - How to check if a word contains one letter from another string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM