我解決了使用這個(效率極其低下的方法)問題的所有字典,有效字謎遊戲:Python-代碼優化幫助 - 查找字
def createList(word, wordList):
#Made a set, because for some reason permutations was returning duplicates.
#Returns all permutations if they're in the wordList
return set([''.join(item) for item in itertools.permutations(word) if ''.join(item) in wordList])
def main():
a = open('C:\\Python32\\megalist.txt', 'r+')
wordList = [line.strip() for line in a]
maximum = 0
length = 0
maxwords = ""
for words in wordList:
permList = createList(words, wordList)
length = len(permList)
if length > maximum:
maximum = length
maxwords = permList
print (maximum, maxwords)
花了類似10分鐘,發現五個字母的單詞這是字典有效的字典。我想用沒有字母限制的單詞來運行它,但需要花費大量的時間。無論如何要優化這個嗎?
+1不錯的展示實際問題如何在數據採集步驟中解決,這使查找/查詢步驟幾乎微不足道。強調初始數據表示的影響。 – ThomasH 2011-05-21 11:27:31
這太棒了;我理解這個想法,儘管我很困惑這些行具體做什麼: d [hash(sortedWord)] .append(word) maxKey = max(d.keys(),key = lambda k:len(d [k])) 你能解釋一下 – Parseltongue 2011-05-21 20:18:34
@Parseltongue,我希望編輯回答你的問題 – 2011-05-22 05:03:36