How can I optimize this Python code to generate all words with word-distance 1?

前端 未结 12 837
予麋鹿
予麋鹿 2021-01-30 22:11

Profiling shows this is the slowest segment of my code for a little word game I wrote:

def distance(word1, word2):
    difference = 0
    for i in range(len(word         


        
12条回答
  •  深忆病人
    2021-01-30 22:23

    Try this:

    def distance(word1, word2):
      return sum([not c1 == c2 for c1, c2 in zip(word1,word2)])
    

    Also, do you have a link to your game? I like being destroyed by word games

提交回复
热议问题