比较Python字典和嵌套字典 [英] Comparing Python dictionaries and nested dictionaries

查看:142
本文介绍了比较Python字典和嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几个类似的问题,但我的问题是非常不同和困难的我。
我有两个字典:

  d1 = {'a':{'b':{'cs' 10','d':{'cs':20}}} 
d2 = {'a':{'b':{'cs':30},'d':{'cs':20 }},'newa':{'q':{'cs':50}}}

d1 具有密钥'a' d2 具有密钥'a''newa'(换句话说 d1 是我的旧dict, d2 是我的新dict)。



我想迭代这些字典,如果密钥是相同的检查其值(嵌套dict),例如当我在 d2 中找到密钥'a'时,我会检查是否有'b ',如果是,则检查值'cs'(从 10 更改为 30 ),如果这个值改变了,我想打印它。



另外一个例子是, code>'newa'从 d2 作为新添加的密钥。



因此,在迭代这两个dict之后,这是预期的输出:

 d2有新的键newa
cs的值从10键改为键b的关键字a

我有以下代码与我,我尝试与许多循环,但不是工作,但也不是一个很好的选择,因此我正在寻找是否可以获得预期的输出与一个递归的代码。

  for k,v in d1.iteritems():
for k1,v1 in d2.iteritems ):
如果k是k1:
打印k
对于v中的k2:
对于v1中的k3:
如果k2是k3:
打印k2,子键匹配

else:
打印抱歉找不到匹配


解决方案

使用递归比较2个词典:

  d1 = {'a':{'b':{'cs':10},'d' 'cs':20}}} 
d2 = {'a':{'b':{'cs':30},'d':{'cs':20}},'newa' 'q':{'cs':50}}}

def findDiff(d1,d2,path =):
在d1.keys()中的k:
如果不是d2.has_key(k):
打印路径,:
打印k +作为键不在d2,\\\

else:
如果类型(d1 [k])是dict:
如果path ==:
path = k
else:
path = path + - > + k
findDiff(d1 [k],d2 [k],path)
else:
如果d1 [k]!= d2 [k]:
打印路径, :
print - ,k,:,d1 [k]
print+,k,:,d2 [k]

print将d1与d2进行比较:
print findDiff(d1,d2)
print将d2与d1进行比较:
print findDiff(d2,d1)

输出:

 将d1与d2进行比较: 
a-> b:
- cs:10
+ cs:30

将d2与d1进行比较:
a-> b:
- cs:30
+ cs:10
a:
newa as key not in d2



I know there are several similar questions out there, but my question is quite different and difficult for me. I have two dictionaries:

d1 = {'a': {'b': {'cs': 10}, 'd': {'cs': 20}}}
d2 = {'a': {'b': {'cs': 30}, 'd': {'cs': 20}}, 'newa': {'q': {'cs': 50}}}

i.e. d1 has key 'a', and d2 has keys 'a' and 'newa' (in other words d1 is my old dict and d2 is my new dict).

I want to iterate over these dictionaries such that, if the key is same check for its value (nested dict), e.g. when I find key 'a' in d2, I will check whether there is 'b', if yes check value of 'cs' (changed from 10 to 30), if this value is changed I want to print it.

Another case is, I want to get key 'newa' from d2 as the newly added key.

Hence, after iterating through these 2 dicts, this is the expected output:

"d2" has new key "newa"
Value of "cs" is changed from 10 to 30 of key "b" which is of key "a"

I have the following code with me, I am trying with many loops which are not working though, but is not a good option too, hence I am looking to find whether I can get expected output with a recursive piece of code.

for k, v in d1.iteritems():
    for k1, v1 in d2.iteritems():
        if k is k1:
            print k
            for k2 in v:
                for k3 in v1:
                    if k2 is k3:
                        print k2, "sub key matched"

        else:
            print "sorry no match found"

解决方案

comparing 2 dictionaries using recursion:

d1= {'a':{'b':{'cs':10},'d':{'cs':20}}}
d2= {'a':{'b':{'cs':30} ,'d':{'cs':20}},'newa':{'q':{'cs':50}}}

def findDiff(d1, d2, path=""):
    for k in d1.keys():
        if not d2.has_key(k):
            print path, ":"
            print k + " as key not in d2", "\n"
        else:
            if type(d1[k]) is dict:
                if path == "":
                    path = k
                else:
                    path = path + "->" + k
                findDiff(d1[k],d2[k], path)
            else:
                if d1[k] != d2[k]:
                    print path, ":"
                    print " - ", k," : ", d1[k]
                    print " + ", k," : ", d2[k] 

print "comparing d1 to d2:"
print findDiff(d1,d2)
print "comparing d2 to d1:"
print findDiff(d2,d1)

Output:

comparing d1 to d2:
a->b :
 -  cs  :  10
 +  cs  :  30
None
comparing d2 to d1:
a->b :
 -  cs  :  30
 +  cs  :  10
a :
newa as key not in d2 

None

这篇关于比较Python字典和嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆