如何获取一个字符串并返回一个字典中所有单词的列表,这些单词与这个单词只有一个字母不同?

时间:2014-11-23 20:18:01

标签: python search dictionary

所以现在我正在使用A-Z的一长串词典。使用这个字典,我试图创建一个函数,该函数将字符串作为参数,并返回该字典中所有单词在任何点都不同的单词。例如。

>>> 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?

是否有人能够帮助我更好地理解这一点?我一直在努力寻找合适的解决方案,感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

我猜你不应该重新发明轮子。有一个很好的python library实现了Levenstein distance指标。我想你会发现它很有用。

答案 1 :(得分:1)

让我们定义一个名为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(相差一个字母)的单词。这是一个功能。它需要两个参数:要比较的词,称为mywordwordlist

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正在寻找wordlist中与hand相差一个字母的字词:

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 comprehension`:

[word for word in wordlist if close_enough(word, myword)]

只有当 wordlist返回True时,它才会遍历close_enough中的每个单词并将其包含在最终列表中。