Python 程序运行太慢

Python 程序运行太慢,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我创建了一个程序来查找拼字游戏中最好的单词,但是当它对照SOWPOD字典检查所有可能的单词列表时,它需要花费很长时间才能完成。我还没有看到它在给出7个字母时成功地完成了算法 这是我的密码: import itertools Dictionary = open("Sowpods.txt").read().splitlines() rackLetters = [] realwords = [] allwords = [] pointValues = {"Blank":0, "A":1, "B":3,

我创建了一个程序来查找拼字游戏中最好的单词,但是当它对照SOWPOD字典检查所有可能的单词列表时,它需要花费很长时间才能完成。我还没有看到它在给出7个字母时成功地完成了算法 这是我的密码:

import itertools
Dictionary = open("Sowpods.txt").read().splitlines()
rackLetters = []
realwords = []
allwords = []
pointValues = {"Blank":0, "A":1, "B":3, "C":3, "D":2, "E":1, "F":4, "G":2, "H":4, "I":1, "J":8, "K":5, "L":1, "M":3, "N":1, "O":1,
               "P":3, "Q":10, "R":1, "S":1, "T":1, "U":1, "V":4, "W":4, "X":8, "Y":4, "Z":10}

def isword(word): #Checks if the give word is in the dictionary
    if word in Dictionary:
        return True
    else:
        return False

def pointcalc(wordlist): #Finds value of a word
    y = 0
    for x in range(0, len(wordlist)):
        y += pointValues[wordlist[x]]
    return y

numoftiles = int(input("How many tiles are in your rack? ")) #Finds number of tiles in user's rack

try:
    int(numoftiles) #Ensures that the number is not a decimal
except:
    print("The amount of tiles in your rack must be a whole number.")
    exit(0)

if numoftiles > 7 or numoftiles < 1: #Ensures that the player has a valid amount of scrabble tiles
    print("Number of tiles in rack must be between inclusive 1 and 7.")
    exit(0)

for x in range(0, numoftiles):
    letter = input("Letter #" + str(x+1) + "? ").upper()
    if len(letter) != 1:
        exit(0)
    rackLetters.append(letter) #Creates List of letters in user's rack

for x in range(0, numoftiles):
    permutations = list(itertools.permutations(rackLetters, x+1))
    allwords.extend(permutations)
print(allwords)
for x in range(0, len(allwords)):
    concat = ''.join(allwords[x]) #Joins list of rack letters into one word
    if isword(concat):
        realwords.append(concat)

for x in range(0, len(realwords)):
    print(realwords[x] + "-" + str(pointcalc(list(realwords[x]))))
导入itertools
Dictionary=open(“Sowpods.txt”).read().splitlines()
rackLetters=[]
realwords=[]
allwords=[]
点值={“空白”:0,“A”:1,“B”:3,“C”:3,“D”:2,“E”:1,“F”:4,“G”:2,“H”:4,“I”:1,“J”:8,“K”:5,“L”:1,“M”:3,“N”:1,“O”:1,
“P:3,Q:10,R:1,S:1,T:1,U:1,V:4,W:4,X:8,Y:4,Z:10}”
def isword(word):#检查给定单词是否在词典中
如果字典中有单词:
返回真值
其他:
返回错误
def pointcalc(单词列表):#查找单词的值
y=0
对于范围(0,len(单词列表))中的x:
y+=点值[wordlist[x]]
返回y
numoftiles=int(输入(“机架中有多少个磁贴?”)#查找用户机架中的磁贴数量
尝试:
int(numoftiles)#确保数字不是小数
除:
打印(“机架中的瓷砖数量必须为整数。”)
出口(0)
如果numoftiles>7或numoftiles<1:#确保玩家拥有有效数量的拼字块
打印(“机架中的瓷砖数量必须介于1和7之间。”)
出口(0)
对于范围内的x(0,numoftiles):
字母=输入(“字母#”+str(x+1)+“?”)。上()
如果len(字母)!=1:
出口(0)
rackLetters.append(letter)#创建用户机架中的字母列表
对于范围内的x(0,numoftiles):
排列=列表(itertools.排列(rackLetters,x+1))
allwords.extend(排列)
打印(所有文字)
对于范围(0,len(allwords))中的x:
concat=''.join(allwords[x])#将框架字母列表合并为一个单词
如果是iWord(concat):
realwords.append(concat)
对于范围(0,len(实字))中的x:
打印(realwords[x]+“-”+str(pointcalc(list(realwords[x])))
创建一组单词。集合中的查找为O(1):

在该行中,创建集合:

Dictionary = set(open("Sowpods.txt").read().splitlines())
使用
set()

例如:

How many tiles are in your rack? 7
Letter #1? d
Letter #2? e
Letter #3? f
Letter #4? g
Letter #5? a
Letter #6? f
Letter #7? e

[('D',), ('E',), ('F',), ('G',), ('A',), ('F',), ('E',), ...
DE-3
DA-3
DE-3
ED-3
EF-5
EA-2
EF-5
EE-2
...