Python dictdiffer模块比较两个字典

Dictdiffer是一个Python模块,用于帮助比较和修改字典。它提供了一个简单的API来找到两个字典之间的差异,并应用这些差异。此模块支持设置容忍度以在比较数值时考虑接近的值相等,还支持处理numpy数组和多种数据结构。
摘要由CSDN通过智能技术生成

http://dictdiffer.readthedocs.io/en/latest/

 

Dictdiffer

https://img.shields.io/travis/inveniosoftware/dictdiffer.svg https://img.shields.io/coveralls/inveniosoftware/dictdiffer.svg https://img.shields.io/github/tag/inveniosoftware/dictdiffer.svg https://img.shields.io/pypi/dm/dictdiffer.svg https://img.shields.io/github/license/inveniosoftware/dictdiffer.svg

Dictdiffer is a helper module that helps you to diff and patch dictionaries.

Installation

Dictdiffer is on PyPI so all you need is:

$ pip install dictdiffer

Usage

Let’s start with an example on how to find the diff between two dictionaries using diff() method:

from dictdiffer import diff, patch, swap, revert

first = {
      
    "title": "hello",
    "fork_count": 20,
    "stargazers": ["/users/20", "/users/30"],
    "settings": {
      
        "assignees": [100, 101, 201],
    }
}

second = {
      
    "title": "hellooo",
    "fork_count": 20,
    "stargazers": ["/users/20", "/users/30", "/users/40"],
    "settings": {
      
        "assignees": [100, 101, 202],
    }
}

result = diff(first, second)

assert list(result) == [
    ('change', ['settings', 'assignees', 2], (201, 202)),
    ('add', 'stargazers', [(2, '/users/40')]),
    ('change', 'title', ('hello', 'hellooo'))]

Now we can apply the diff result with patch() method:

result = diff(first, second)
patched = patch(result, first)

assert patched == second

Also we can swap the diff result with swap() method:

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值