- 论坛徽章:
- 0
|
回复 7# yyr321677
试着写了一个,不知道是否满足你的要求。- dic1={'D:': {'Sony': {'Version': 250151506}, 'APP': {'Ecplise': {'workspace': {'Tripwire': {'test2': 9999999}}}}}}
- dic2={'D:': {'APP': {'Ecplise': {'workspace': {'Tripwire': {'test1': 45648974423}}}}}}
- def merge(a,b):
- if type(a)== type({}) and type(b) == type({}):
- ak = set(a.keys())
- bk = set(b.keys())
- c = ak & bk
- d = ak - bk
- e = bk - ak
- fd = {}
- for c0 in c:
- fd[c0] = merge(a[c0],b[c0])
- for d0 in d:
- fd[d0] = a[d0]
- for e0 in e:
- fd[e0] = b[e0]
- return fd
- dic3 = merge(dic1,dic2)
- def delete(a,b):
- if type(a) == type({}) and type(b) == type({}):
- ak = set(a.keys())
- bk = set(b.keys())
- c = ak & bk
- d = ak - bk
- e = bk - ak
- fd = {}
- for c0 in c:
- fd[c0] = delete(a[c0],b[c0])
- if len(fd[c0]) == 0 :
- del fd[c0]
- for d0 in d:
- fd[d0] = a[d0]
- for e0 in e:
- raise ValueError("The dic B not in dic A")
- return fd
- return {}
- print dic2
- print dic3
- print delete(dic3,dic2)
复制代码 结果:- {'D:': {'APP': {'Ecplise': {'workspace': {'Tripwire': {'test1': 45648974423}}}}}}
- {'D:': {'Sony': {'Version': 250151506}, 'APP': {'Ecplise': {'workspace': {'Tripwire': {'test1': 45648974423, 'test2': 9999999}}}}}}
- {'D:': {'Sony': {'Version': 250151506}, 'APP': {'Ecplise': {'workspace': {'Tripwire': {'test2': 9999999}}}}}}
复制代码 |
|