• Python 比较两个字典


    一、简单字典比较

    这里的简单字典比较,指的是没有嵌套字典,仅仅只有一层的意思。

    1、比较字典的值

    first_dict = {
        "name": "鸣人",
        "age": 22,
        "sex": "",
        "title": "六代火影",
        "heigh": "179cm"
    }
    
    second_dict = {
        "name": "鸣人",
        "age": 22,
        "sex": "",
        "title": "六代火影"
    }
    
    keys = set(first_dict) - set(second_dict)
    print(keys)

    结果

    {'heigh'}

    注意这里是first_dict键要多余second_dict时候才会有值,反之没有,如下:

    second_dict = {
        "name": "鸣人",
        "age": 22,
        "sex": "",
        "title": "六代火影",
        "heigh": "179cm"
    }
    
    first_dict = {
        "name": "鸣人",
        "age": 22,
        "sex": "",
        "title": "六代火影"
    }
    
    keys = set(first_dict) - set(second_dict)
    print(keys)

    结果

    set()

    2、比较键值

    second_dict = {
        "name": "鸣人",
        "age": 22,
        "sex": "",
        "title": "六代火影",
        "heigh": "179cm"
    }
    
    first_dict = {
        "name": "鸣人",
        "age": 22,
        "sex": "",
        "title": "六代火影"
    }
    
    value = { k : second_dict[k] for k in set(second_dict) - set(first_dict) }
    print(value)

    结果

    {'heigh': '179cm'}

    注意这里是first_dict键要多余second_dict时候才会有值,反之没有

    其他的写法:

    second_dict = {
        "name": "鸣人",
        "age": 22,
        "sex": "",
        "title": "六代火影"
    }
    
    first_dict = {
        "name": "鸣人",
        "age": 22,
        "sex": "",
        "title": "六代火影",
        "aaaa": 123
    }
    
    set1 = set(first_dict.items())
    set2 = set(second_dict.items())
    print(set1 ^ set2)

    结果

    {('sex', ''), ('sex', ''), ('aaaa', 123)}

    二、复杂字典比较

    这里的复杂字典比较,指的是有嵌套的字典,以嵌套两层的字典为例。

    1、利用第三方库比较(dictdiffer)

    import dictdiffer
    
    second_dict = {
        "template": "",
        "template2": "",
        "data": {
            "name": "鸣人",
            "age": 22,
            "sex": "",
            "title": "六代火影"
        }  # 数据
    }
    
    first_dict = {
        "template": "11",
        "template1": "11",
        "data": {
            "name": "鸣人",
            "age": 22,
            "sex": "",
            "title": "六代火影"
        }  # 数据
    }
    
    for diff in list(dictdiffer.diff(first_dict, second_dict)):
        print(diff)

    结果

    ('change', 'template', ('11', '')) #值不同
    ('change', 'data.sex', ('', '')) #嵌套字典值不同
    ('add', '', [('template2', '')]) #多的键值对
    ('remove', '', [('template1', '11')]) #少的键值对

    2、自定义函数

    def dict_diff(first, second):
        """
        比较两个字典的不同
        """
        KEYNOTFOUNDIN1 = '<KEYNOTFOUNDIN1>'  # 没在第一个字典中找到
        KEYNOTFOUNDIN2 = '<KEYNOTFOUNDIN2>'  # 没在第二个字典中找到
        
        diff = {}
        sd1 = set(first)
        sd2 = set(second)
        # 第二个字典中不存在的键
        for key in sd1.difference(sd2):
            diff[key] = KEYNOTFOUNDIN2
        # 第一个字典中不存在的键
        for key in sd2.difference(sd1):
            diff[key] = KEYNOTFOUNDIN1
        # 比较不同
        for key in sd1.intersection(sd2):
            if first[key] != second[key]:
                diff[key] = (first[key], second[key])
        return diff
        
    if __name__ == '__main__':
        data1 = {
            "template": "",
            "template2": "",
            "data": {
                "name": "鸣人",
                "age": 22,
                "sex": "",
                "title": "六代火影"
            }  # 数据
        }
        data2 = {
            "template": "11",
            "template1": "11",
            "data": {
                "name": "鸣人",
                "age": 22,
                "sex": "",
                "title": "六代火影"
            }  # 数据
        }
        print(dict_diff(data1, data2))

    结果

    {'template2': '<KEYNOTFOUNDIN2>', 'template1': '<KEYNOTFOUNDIN1>', 'template': ('', '11'), 'data': ({'name': '鸣人', 'age': 22, 'sex': '', 'title': '六代火影'}, {'name': '鸣人', 'age': 22, 'sex': '', 'title': '六代火影'})}
  • 相关阅读:
    HDOJ 5090 Game with Pearls 二分图匹配
    hdu4360 spfa+分割点
    分布式高级(十三)Docker Container之间的数据共享
    [Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)
    [Ramda] Create an Array From a Seed Value with Ramda's unfold
    [Flow] Declare types for application
    [Flow] The Fundamentals of Flow
    [Angular] Some performance tips
    [Ramda] Rewrite if..else with Ramda ifElse
    [SVG] Add an SVG as an Embedded Background Image
  • 原文地址:https://www.cnblogs.com/gcgc/p/15618117.html
Copyright © 2020-2023  润新知