zoukankan      html  css  js  c++  java
  • 对比多层字典是否相同函数

    现有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) #调用函数
  • 相关阅读:
    Bzoj 1537: [POI2005]Aut- The Bus 题解 [由暴力到正解]
    Bzoj 3126[Usaco2013 Open]Photo 题解
    Bzoj 3165 [Heoi2013]Segment题解
    Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并
    赛前集训前的总结(警醒)
    bzoj3316 JC loves Mkk题解
    9.22考试 crf的军训 题解
    Luogu3521 [POI2011]ROT-Tree Rotations
    CTSC2012 熟悉的文章
    UVA11468 Substring
  • 原文地址:https://www.cnblogs.com/xiaokuangnvhai/p/11202519.html
Copyright © 2011-2022 走看看