相关疑难解决方法(0)

如何使Python Scrabble文字查找器更快?

我没有真正需要改进它,它只是为了好玩.现在它在大约200K字的列表上花了大约一秒钟.

我已经尝试过尽可能地优化它(使用生成器代替列表推导产生了很大的不同),并且我已经没有想法了.

你有什么?

#!/usr/bin/env python
# let's cheat at scrabble

def count_letters(word):
  count = {} 
  for letter in word:
    if letter not in count: count[letter] = 0
    count[letter] += 1 
  return count 

def spellable(word, rack):
    word_count = count_letters(word)
    rack_count  = count_letters(rack)
    return all( [word_count[letter] <= rack_count[letter] for letter in word] )  

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
         "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
         "l": 1, "o": …
Run Code Online (Sandbox Code Playgroud)

python optimization

23
推荐指数
1
解决办法
2万
查看次数

标签 统计

optimization ×1

python ×1