【发布时间】:2014-12-24 12:56:51
【问题描述】:
所以我想做的是创建一个字典:
- key 是排序后的单词,
- value 是每个字谜的集合(由字谜程序生成)。
当我运行我的程序时,我得到了 Ex。单词:{('w','o','r','d')} 不是单词:dorw,wrdo,rowd。文本文件只包含很多单词,每行一个。
代码:
def main():
wordList = readMatrix()
print(lengthWord())
def readMatrix():
wordList = []
strFile = open("words.txt", "r")
lines = strFile.readlines()
for line in lines:
word = sorted(line.rstrip().lower())
wordList.append(tuple(word))
return tuple(wordList)
def lengthWord():
lenWord = 4
sortDict = {}
wordList = readMatrix()
for word in wordList:
if len(word) == lenWord:
sortWord = ''.join(sorted(word))
if sortWord not in sortDict:
sortDict[sortWord] = set()
sortDict[sortWord].add(word)
return sortDict
main()
【问题讨论】:
-
s = "word" {s: [{"".join(tup) } for tup in (permutations(s, len(s)))]}