首页 > 解决方案 > 浏览文本文件

问题描述

我一直在尝试创建一个功能,为扮演刽子手的用户提供提示。该函数背后的想法是我有一个包含 5k 个单词的列表,我需要通过众多指标对其进行排序,例如单词应该与模式相同,如果模式a___le如此,那么我应该寻找假设在同一个组中,并且如果用户有许多错误的字母,它不会考虑包含这些字母的单词

我知道我没有以最pythonic或最优雅的方式做到这一点,但如果有人能告诉我这里出了什么问题,我总是得到一个空列表或包含长度相同但另一个单词的列表条件一直被忽视。

def filter_words_list(words, pattern, wrong_guess_lst):
    """
    :param words: The words I received from the main function 
    :param pattern: the pattern of the word in seach such as p__pl_
    :param wrong_guess_lst: the set of wrong used letters of the users
    :return: the function returns the words which are much to the conditions.
    """
    list(wrong_guess_lst) # Since I am receiving it as a set I'm converting it to a list.
    words_suggestions = [] # The list I'd like to put my suggested words.
    for i in range(0, len(words)): # First loop matching between the length of the patterns and the words
        if len(words[i]) == len(pattern):
            for j in range(0, len(pattern)):
                if pattern[j] != '_':
                    if pattern[j] == words[i][j]: # Checking if the letters of the words are a much.
                        for t in range(0, len(wrong_guess_lst)):
                         if wrong_guess_lst[t] != words[i][j]: # Does the same as before but only with the wrong guess lst.
                            words_suggestions.append(words[i])
    return words_suggestions

标签: pythonlistpython-3.6

解决方案


我认为这就是您正在寻找的(代码注释中的解释):

def get_suggestions(words: list, pattern: str, exclude: list) -> list:
    """Finds pattern and returns all words matching it."""
    # get the length of the pattern for filtering
    length = len(pattern)
    # create a filtered generator so that memory is not take up;
    # it only give the items from the word list that match the
    # conditions i.e. the same length as pattern and not in excluded words
    filter_words = (word for word in words
                    if len(word) == length and word not in exclude)
    # create a mapping of all the letters and their corresponding index in the string
    mapping = {i: letter for i, letter in enumerate(pattern) if letter != '_'}
    # return list comprehension that is made of words in the filtered words that
    # match the condition that all letters are in the same place and have the same
    # value as the mapping
    return [word for word in filter_words
            if all(word[i] == v for i, v in mapping.items())]


word_list = [
    'peach', 'peace', 'great', 'good', 'food',
    'reach', 'race', 'face', 'competent', 'completed'
]
exclude_list = ['good']
word_pattern = 'pe___'

suggestions = get_suggestions(word_list, word_pattern, exclude_list)
print(suggestions)
# output:
# ['peach', 'peace']


# a bit of testing
# order of items in the list is important
# it should be the same as in the word_list
patterns_and_answers = {
    '_oo_': ['food'],  # 'good' is in the excluded words
    '_omp_____': ['competent', 'completed'],
    '__ce': ['race', 'face'],
    'gr_a_': ['great'],
    '_a_e': ['race', 'face'],
    '_ea__': ['peach', 'peace', 'reach']
}

for p, correct in patterns_and_answers.items():
    assert get_suggestions(word_list, p, exclude_list) == correct
print('all test cases successfull')

推荐阅读