python deepdiff和pprint

python deepdiff和pprintpythondeepdi 官方文档 https pypi org project deepdiff deepdiff 的作用详解 r DeepDiffcomm iterables stringsandal Itwi

python deepdiff 官方文档:https://pypi.org/project/deepdiff/

deepdiff的作用详解:

r""" DeepDiff comments Deep Difference of dictionaries, iterables, strings and almost any other object. It will recursively look for all the changes. Parameters t1 : A dictionary, list, string or any python object that has __dict__ or __slots__ This is the first item to be compared to the second item t2 : dictionary, list, string or almost any python object that has __dict__ or __slots__ The second item is to be compared to the first one ignore_order : Boolean, defalt=False ignores orders for iterables. Note that if you have iterables contatining any unhashable, ignoring order can be expensive. Ignoring order for an iterable containing any unhashable will include duplicates if there are any in the iterable. Ignoring order for an iterable containing only hashables will not include duplicates in the iterable. Returns A DeepDiff object that has already calculated the difference of the 2 items. Supported data types int, string, unicode, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple and custom objects! Examples Importing >>> from base.deepdiff import DeepDiff >>> from pprint import pprint >>> from __future__ import print_function # In case running on Python 2 Same object returns empty >>> t1 = {1:1, 2:2, 3:3} >>> t2 = t1 >>> print(DeepDiff(t1, t2)) {} Type of an item has changed >>> t1 = {1:1, 2:2, 3:3} >>> t2 = {1:1, 2:"2", 3:3} >>> pprint(DeepDiff(t1, t2), indent=2) { 'type_changes': { 'root[2]': { 'newtype': 
  
    , 'newvalue': '2', 'oldtype': 
   
     , 'oldvalue': 2}}} Value of an item has changed >>> t1 = {1:1, 2:2, 3:3} >>> t2 = {1:1, 2:4, 3:3} >>> pprint(DeepDiff(t1, t2), indent=2) {'values_changed': {'root[2]': {'newvalue': 4, 'oldvalue': 2}}} Item added and/or removed >>> t1 = {1:1, 2:2, 3:3, 4:4} >>> t2 = {1:1, 2:4, 3:3, 5:5, 6:6} >>> ddiff = DeepDiff(t1, t2) >>> pprint (ddiff) {'dic_item_added': ['root[5]', 'root[6]'], 'dic_item_removed': ['root[4]'], 'values_changed': {'root[2]': {'newvalue': 4, 'oldvalue': 2}}} String difference >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world"}} >>> t2 = {1:1, 2:4, 3:3, 4:{"a":"hello", "b":"world!"}} >>> ddiff = DeepDiff(t1, t2) >>> pprint (ddiff, indent = 2) { 'values_changed': { 'root[2]': {'newvalue': 4, 'oldvalue': 2}, "root[4]['b']": { 'newvalue': 'world!', 'oldvalue': 'world'}}} String difference 2 >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world!\nGoodbye!\n1\n2\nEnd"}} >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world\n1\n2\nEnd"}} >>> ddiff = DeepDiff(t1, t2) >>> pprint (ddiff, indent = 2) { 'values_changed': { "root[4]['b']": { 'diff': '--- \n' '+++ \n' '@@ -1,5 +1,4 @@\n' '-world!\n' '-Goodbye!\n' '+world\n' ' 1\n' ' 2\n' ' End', 'newvalue': 'world\n1\n2\nEnd', 'oldvalue': 'world!\n' 'Goodbye!\n' '1\n' '2\n' 'End'}}} >>> >>> print (ddiff['values_changed']["root[4]['b']"]["diff"]) --- +++ @@ -1,5 +1,4 @@ -world! -Goodbye! +world 1 2 End Type change >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}} >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world\n\n\nEnd"}} >>> ddiff = DeepDiff(t1, t2) >>> pprint (ddiff, indent = 2) { 'type_changes': { "root[4]['b']": { 'newtype': 
    
      , 'newvalue': 'world\n\n\nEnd', 'oldtype': 
     
       , 'oldvalue': [1, 2, 3]}}} List difference >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3, 4]}} >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2]}} >>> ddiff = DeepDiff(t1, t2) >>> pprint (ddiff, indent = 2) {'iterable_item_removed': {"root[4]['b'][2]": 3, "root[4]['b'][3]": 4}} List difference 2: >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}} >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}} >>> ddiff = DeepDiff(t1, t2) >>> pprint (ddiff, indent = 2) { 'iterable_item_added': {"root[4]['b'][3]": 3}, 'values_changed': { "root[4]['b'][1]": {'newvalue': 3, 'oldvalue': 2}, "root[4]['b'][2]": {'newvalue': 2, 'oldvalue': 3}}} List difference ignoring order or duplicates: (with the same dictionaries as above) >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}} >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}} >>> ddiff = DeepDiff(t1, t2, ignore_order=True) >>> print (ddiff) {} List that contains dictionary: >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, {1:1, 2:2}]}} >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, {1:3}]}} >>> ddiff = DeepDiff(t1, t2) >>> pprint (ddiff, indent = 2) { 'dic_item_removed': ["root[4]['b'][2][2]"], 'values_changed': {"root[4]['b'][2][1]": {'newvalue': 3, 'oldvalue': 1}}} Sets: >>> t1 = {1, 2, 8} >>> t2 = {1, 2, 3, 5} >>> ddiff = DeepDiff(t1, t2) >>> pprint (DeepDiff(t1, t2)) {'set_item_added': ['root[3]', 'root[5]'], 'set_item_removed': ['root[8]']} Named Tuples: >>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y']) >>> t1 = Point(x=11, y=22) >>> t2 = Point(x=11, y=23) >>> pprint (DeepDiff(t1, t2)) {'values_changed': {'root.y': {'newvalue': 23, 'oldvalue': 22}}} 
      
     
    
  

例子:

#!/usr/bin/env python # -*- coding: UTF-8 -*- '''================================================= @Project -> File : -> diff_test @IDE :PyCharm @Author :zhangjun.xue.o @Date :2019-09-17 10:07 @Desc : ==================================================''' from deepdiff import DeepDiff from pprint import pprint t1 = {"one": 1, "two": 2, "three": 3} t2 = {"one": 1, "two": 4, "three": "6"} if __name__ == "__main__": res = DeepDiff(t1, t2) pprint(res) 

输出:

{'type_changes': {"root['three']": {'newtype': 
  
    , 'newvalue': '6', 'oldtype': 
   
     , 'oldvalue': 3}}, 'values_changed': {"root['two']": {'newvalue': 4, 'oldvalue': 2}}} 
    
  

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/227971.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月16日 下午8:05
下一篇 2026年3月16日 下午8:06


相关推荐

  • CentOS如何查看本机ip

    CentOS如何查看本机ip之前我们习惯的 ipconfig 不能用了原来是改了呀 改成了 ipaddress 我们可以输入 ipaddress 也可以输入 ipaddr 或者是 ipadd 你会发现 无法查看 ip 但至少这个命令是存在的你需要打开网卡配置文件 etc sysconfig network scripts ifcfg ens33 将 ONBOOT 修改为 yes 也就是启动网卡 vi et

    2025年10月10日
    5
  • spring整合spring-data-redis和spring-session-data-redis通过shiro实现单点登录

    spring整合spring-data-redis和spring-session-data-redis通过shiro实现单点登录运行效果图缓存说明(本项目没有使用shiro的缓存管理器和session管理器)shiro_user_cache:permission:权限缓存,当前只有test用户shiro_user_cache:role:角色缓存,当前只有test用户shiro_user_kickout:保存被踢出的用户shiro_user_online:保存登录了的用户sprting:spr

    2022年5月3日
    102
  • Elasticsearch 搜索数组字段

    Elasticsearch 搜索数组字段参考文章 Elasticsearc 搜索数组字段 hhhzua 的专栏 CSDN 博客 elasticsearc 查询数组 1 搜索数组字段 tags 中同时存在元素 str a str b query bool filter term tags str a term

    2026年3月17日
    2
  • idea2021 mybatiscodehelper2.9 激活码【2021免费激活】

    (idea2021 mybatiscodehelper2.9 激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~0YQJ1128OW-eyJsaWNlb…

    2022年3月28日
    210
  • 优麒麟 2204 安装 Fcitx5 输入法

    优麒麟 2204 安装 Fcitx5 输入法Fcitx5是继Fcitx之后的新一代输入法框架,优麒麟2204默认安装的是Fcitx,而Fcitx和Fcitx5是相互冲突的,因此安装Fcitx5之前需要先卸载Fcitx。

    2022年7月14日
    27
  • Hmily 源码解析(二)—— 调用微服务

    Hmily 源码解析(二)—— 调用微服务由于篇幅过长,将该模块单独拎出一节,接上文Hmily源码解析(二)——执行主体方法上文我们把主体方法的执行及Feign的相关配置讲解了,知道在调用微服务时把对应的HmilyTransactionContext实例以“HMILY_TRANSACTION_CONTEXT”为key作为请求参数一同发送过来,及调用微服务成功之后会把调用接口的方法(有@Hmily注解的)封装为HmilyPa…

    2022年5月11日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号