在python中的对应索引处比较键和值

时间:2018-09-25 13:44:27

标签: python dictionary match key-value

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"})) 

1 个答案:

答案 0 :(得分:1)

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

{{1}}
相关问题