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

前端 未结 12 838
予麋鹿
予麋鹿 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:21

    for this snippet:

    for x,y in zip (word1, word2):
        if x != y:
            difference += 1
    return difference
    

    i'd use this one:

    return sum(1 for i in xrange(len(word1)) if word1[i] == word2[i])
    

    the same pattern would follow all around the provided code...

提交回复
热议问题