python中有字典,并比较了值拼写。如果错误大于或等于2,则打印错误

input = {“ their”:“ thuyr”}

输出=不正确(因为t = t,h = h但e!= u,i!= y)。

我的问题是我无法比较t == t,h == h,e == u,i == y。
下面的代码显示计数值22,但计数值必须为2,因为只有两个单词与它们不匹配

def find_correct(words_dict):
    count=0
    for key,value in words_dict.items():
        for val in value:
            for ky in key:
                if(val!=ky):
                    count+=1
    return count

print(find_correct({"their":"thuor"}))

最佳答案

这是因为您正在使用嵌套循环。它将“他们”中的每个“ t”字母与“ thuor”中的每个5个字母进行比较。相反,只需使用这样的单个循环:

def find_correct(words_dict):
count=0
for key,value in words_dict.items():
    for val, ky in zip(value, key):
        if(val!=ky):
            count+=1
return count

09-05 01:27
查看更多