【发布时间】:2019-07-19 19:31:54
【问题描述】:
基本上,我从插入单词“brand”开始,将单词中的单个字符替换为下划线,然后尝试查找与其余字符匹配的所有单词。例如:
"b_and" 将返回:"band"、"brand"、"bland" .... 等等。
我开始使用 re.sub 替换字符中的下划线。但我真的不知道下一步该去哪里。我只想要这个下划线不同的单词,要么没有下划线,要么用字母替换。就像如果“under”这个词贯穿整个列表,我不希望它返回“understood”或“thunder”,只是一个字符差异。任何想法都会很棒!
我尝试先用字母表中的每个字母替换字符,然后再检查该单词是否在字典中,但这花了很长时间,我真的想知道是否有更快的方法
from itertools import chain
dictionary=open("Scrabble.txt").read().split('\n')
import re,string
#after replacing the word with "_", we find words in the dictionary that match the pattern
new=[]
for letter in string.ascii_lowercase:
underscore=re.sub('_', letter, word)
if underscore in dictionary:
new.append(underscore)
if new == []:
pass
else:
return new
【问题讨论】: