码迷,mamicode.com
首页 > 其他好文 > 详细

对比多层字典是否相同函数

时间:2019-07-17 18:55:02      阅读:27      评论:0      收藏:0      [点我收藏+]

标签:not   style   key   int   video   tag   多层   for   stat   

 

现有2个字典且是多层级的,找出2个字典的不同且输出,实现如下:

ok_req={
    "version": "9.0.0",
    "is_test": True,
    "store": "",
    "urs": "",
    "device": {
        "os": "android",
        "imei": "99001062198893",
        "device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
        "mac": "02:00:00:00:00:00",
        "galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
        "udid": "a34b1f67dd5797df93fdd8b072f1fb8110fd0db6",
        "network_status": "wifi"
    },
    "adunit": {
        "category": "VIDEO",
        "location": "1",
        "app": "7A16FBB6",
        "blacklist": ""
    },
    "ext_param":{
        "is_start" : 0,
        "vId":"VW0BRMTEV"
    }
}
not_ok={
    "version": "9.0.0",
    "is_test": True,
    "urs": "",
    "store": "",
    "device": {
        "os": "android",
        "imei": "99001062298893",
        "device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
        "mac": "02:00:00:00:00:00",
        "galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
        "udid": "a34b1f67dd5797da93fdd8b072f1fb8110fd0db6",
        "network_status": "wifi"
    },
    "adunit": {
        "category": "VIDEO",
        "location": "1",
        "app": "7A16FBB6",
        "blacklist": ""
    },"ext_param": {
        "is_start": 0,
        "vid": "VW0BRMTEV"
    }
}
#对比多层字典函数
def dic_compare(dic_name,compar_dic):
    for key, value in dic_name.items():  #遍历字典
        if key not in compar_dic.keys(): #如果KEY不在对比的字典中,输出字典
            print(key, value)
        if type(value) != dict: #如果value不是多层
            if value not in compar_dic.values(): #不是多层的,如果value和对比字典不一致输出
                print(key, value)
        if  type(value) == dict: #如果value是多层的,那么通过递归再次走上面代码进行对比
            dic_compare(value,compar_dic[key])

dic_compare(ok_req,not_ok) #调用函数

 

对比多层字典是否相同函数

标签:not   style   key   int   video   tag   多层   for   stat   

原文地址:https://www.cnblogs.com/xiaokuangnvhai/p/11202519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!