匹配字典中的键和值

2024-10-02 10:21:39 发布

您现在位置:Python中文网/ 问答频道 /正文

每个键都与值进行比较(可以说键和值之间有拼写检查)。如果只有两个单词不匹配,则打印该键

input={“their”:“thuor”,“diksha”,“dijmno”} 输出=[“他们的”]

def find_correct(words_dict):
    count=0
    final_list=[]
    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 
            if(count<=2):
                final_list.append(key)

    return final_list

print(find_correct({"their":"thuor","diksha":"dijmno"}))

Tags: keyinforvaluecountfinddictlist

热门问题