比较Python中相应索引处的键和值 [英] Compare key and values at corresponding index in Python

查看:54
本文介绍了比较Python中相应索引处的键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python.key中有字典,比较值拼写。如果错误大于或等于2,则打印不正确



input = {他们的: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

表示密钥,其值为words_dict.items():

表示值:

表示关键字:

if(val!= ky):

count + = 1

返回计数



print(find_correct({their:thuor}))

There is dictionary in python.key and value spelling is compared.if the mistake is greater than or equal to 2 than print incorrect

input={"their":"thuyr"}

output=incorrect(because t=t,h=h but e!=u,i!=y).

My problem is that i was unable to compare t==t,h==h,e==u,i==y. The below code shows count value 22 but count value must be 2 because only two words mismatches with their

What I have tried:

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

推荐答案

问题是您有两个循环,因此对于值中的每个字符,您要与键中的每个字符进行比较。这使得25个实际测试,其中22个将不相等。 Chang到以下内容:

The problem is that you have two loops, so for each character in the value you are comparing against each character in the key. That makes 25 actual tests, and 22 of them will be not equal. Chang to the following:
def find_correct(words_dict):
    count=0
    for key,value in words_dict.items():
        for i in range(len(value)): # this may need adjusting for different length words
            if(value[i]!=key[i]):
                count+=1 
    return count 

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


这篇关于比较Python中相应索引处的键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆