比较python中相应索引处的键和值
python中有字典。比较键和值的拼写。如果错误大于或等于2,则打印错误 输入={“他们的”:“thuyr”} 输出=不正确(因为t=t,h=h,但e!=u,i!=y) 我的问题是我无法比较t==t,h==h,e==u,i==y。 下面的代码显示了计数值22,但计数值必须为2,因为只有两个字与它们的值不匹配比较python中相应索引处的键和值,python,dictionary,match,key-value,Python,Dictionary,Match,Key Value,python中有字典。比较键和值的拼写。如果错误大于或等于2,则打印错误 输入={“他们的”:“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
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”的每个字母与“Thour”中的每个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