【发布时间】:2014-12-12 17:20:52
【问题描述】:
我正在尝试创建一个列表字典,其中键是字谜,值(列表)包含该字谜中所有可能的单词。
所以我的 dict 应该包含这样的内容
{'aaelnprt': ['parental', 'paternal', 'prenatal'], ailrv': ['rival']}
可能的单词在 .txt 文件中。每个单词都用换行符分隔。示例
Sad
Dad
Fruit
Pizza
当我尝试对其进行编码时,这会导致问题。
with open ("word_list.txt") as myFile:
for word in myFile:
if word[0] == "v": ##Interested in only word starting with "v"
word_sorted = ''.join(sorted(word)) ##Get the anagram
for keys in list(dictonary.keys()):
if keys == word_sorted: ##Heres the problem, it doesn't get inside here as theres extra characters in <word_sorted> possible "\n" due to the linebreak of myfi
print(word_sorted)
dictonary[word_sorted].append(word)
【问题讨论】: