参数: dict1, dict2

需求:如果dict1和dict2中有不同的key,那么返回这个(key, dict1[key]);如果dict1和dict2中有相同的key,但是value不同,返回这个(key, dict1[key])

实现:

def dict_different_data(first, second):
"""
get the different data bewtten two dicts objects
return :result = first - second """
assert isinstance(first, dict)
assert isinstance(second, dict)
different_data = {}
update_key = set(first).intersection(set(second))
insert_key = set(first).difference(set(second))
delet_key = set(second).difference(set(first)) #updata data item which are both on first and second and Not equal values
for k in update_key:
if(isinstance(first[k], dict)):
result = dict_different_data(first[k], second[k])
if len(result) > 0:
different_data[k] = result
elif first[k] != second[k]:
different_data[k] = first[k]
#insert new item from first
for k in insert_key:
different_data[k] = first[k]
#delet data
for k in delet_key :
different_data[k] = None return different_data if __name__ == "__main__":
dic1 = {
'name': 'test',
'score': 89
}
dic2 = {
'name': 'test',
'score': 29,
'age': 23
}
result = dict_different_data(dic2, dic1)
print result

python获取两个dict的不同的更多相关文章

  1. python获取两个日期间的工作日

    import datetime # 计算两个日期之间的工作日数,非天数. class workDays(): def __init__(self, start_date, end_date, days ...

  2. Python获取两个ip之间的所有ip

    int_ip = lambda x: '.'.join([str(x/(256**i)%256) for i in range(3,-1,-1)]) ip_int = lambda x:sum([25 ...

  3. Python获取两个文件的交集、并集、差集

    题记:朋友在处理数据时,需要解决这方面的问题,所以利用她给的代码,自己重新梳理了下,并成功运行. 代码如下: # coding:utf-8 s1 = set(open(r'C:\\Users\\yan ...

  4. python 获取两位的月份(09)和天数(09)

  5. python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作

    转自:http://blog.chinaunix.net/uid-200142-id-3992553.html 有时候,为了需求,需要统计两个 list 之间的交集,并集,差集.查询了一些资料,现在总 ...

  6. python获取字母在字母表对应位置的几种方法及性能对比较

    python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...

  7. python 获取日期

    转载   原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...

  8. Python中list,tuple,dict,set的区别和用法

    Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...

  9. Python获取目录、文件的注意事项

    Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...

随机推荐

  1. The Swift.org Blog welcome欢迎页note

    The Swift.org Blog Welcomehtml, body {overflow-x: initial !important;}html { font-size: 14px; } body ...

  2. ResponderChain note

    http://ww3.sinaimg.cn/large/6b288462gw1evl4h40tfxj20sg0lc77k.jpg

  3. CentOS6.3升级Python到2.7.3版本

    http://www.zhangchun.org/the-centos6-3-upgrade-python-to-2-7-3-version/ 查看python的版本 1 python  -V  2 ...

  4. Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat

    一.Linxu服务器远程启动tomcat 1.首先确保linxu服务器上的tomcat jdk等必要软件正确安装,并且可以正常启动. 2.编写Java SSH工具类. 相关jar包: <depe ...

  5. Android——listview android:cacheColorHint,android:listSelector属性作用

    ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉, 原因是,滚动时,列表里面的view重绘时,用的依 ...

  6. c#获取网页代码、数据、资源

    //WebClient取网页源码 private string GetHtmlSource(string Url) { try { System.Net.WebClient wc = new Syst ...

  7. mongo 杀掉慢的程序killMyRunningOps("12.23.32.21") #####这个是客户端的ip

    mongodb运维(3) db.currentOp与db.killOp命令 2018.08.12 23:55 113浏览  字号 好久没更新mongo运维这块知识了,这次介绍 db.currentOp ...

  8. C51寄存器

    EA   全局中断允许位 ET2 定时器/计时器2中断允许位 ES 串行口中断允许位 ET1 定时器/计时器1中断允许位 EX1 外部中断1中断允许位 ET0 定时器/计时器0中断允许位 EX0 外部 ...

  9. latin-1 codec cant encode characters in position 42-48: ordinal not in range256 下载文件时候报错

    python后端写下载文件, 这个时候出现了这个错误 latin-1 codec cant encode characters in position 42-48: ordinal not in ra ...

  10. Spring面试,IoC和AOP的理解

    spring 的优点?1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实 ...