-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_data.py
78 lines (71 loc) · 1.72 KB
/
merge_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# coding:utf-8
author = "Guanjinglin"
email = "[email protected]"
# Created on 2018-09-12
# 本代码基于python3.6.1测试通过
# 通过递归合并两条内部多种数据结构的字典
a = {
"name": '111',
"url": "123123123",
"tag": {
"name": "2222",
"url": "bbb",
"ccc": "xxx",
"tag1": {
"name": "22222",
"url": "xxx"
},
"tag2": {
"name": "666666",
"url": "yyy",
"666": "66666"
},
},
"test1": [
"1", "2", "3", "666", {
"aa": "11",
"bb": "22"
},
]
}
b = {
"name": "222",
"url": "23213123",
"tag": {
"name": "1111",
"url": "bbb",
"tag1": {
"name": "11111",
"url": "xxx"
},
"tag2": {
"name": "dwdqw",
"url": "yyy"
}
},
"test1": [
"2", "3", "4", "5", {
"bb": "22",
"cc": "333"
}
]
}
def a_b(a, b): # 两条数据, 以第一条数据为优先,进行数据合并
if isinstance(a, dict) and isinstance(b, dict):
for i, j in a.items():
if i not in b:
b[i] = j
else:
if isinstance(j, list) or isinstance(j, dict):
a_b(a[i], b[i])
if isinstance(a, list) and isinstance(b, list):
for i in range(len(a)):
if not a[i] in b and not isinstance(a[i], dict) and not isinstance(a[i], list):
b.append(a[i])
else:
if isinstance(a[i], list) or isinstance(a[i], dict):
a_b(a[i], b[i])
return b
if __name__ == '__main__':
x = a_b(a, b)
print(x)