python - 浏览文本文件
问题描述
我一直在尝试创建一个功能,为扮演刽子手的用户提供提示。该函数背后的想法是我有一个包含 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
解决方案
我认为这就是您正在寻找的(代码注释中的解释):
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')
推荐阅读
- database - 由于超出了允许的最大数据库大小,Oracle 数据库无法打开
- c++ - 如何改进我的文件写入方法以减小 Wavefront Object 文件大小?
- javascript - MongoDB 中的 GET 请求
- neo4j - Neo4j 使用 JACCARD 或 sorensenDiceSimilarity 或类似的东西在节点上查找字符串的相似性
- oauth - 如何在 Nest.js 身份验证流程中传递状态
- javascript - 箭头函数:这个块解释了什么?
- amazon-web-services - FindInMap 是否在 Fn::If 中使用数组选择工作?
- android - 软键盘在DialogFragment中隐藏AutoCompleteTextView的下拉菜单
- docker - docker-entrypoint-initdb.d 上的 Arangodb 连接失败
- c# - Linq 填充 Treeview 控件:实时过滤