-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path__init__.py
123 lines (119 loc) · 2.48 KB
/
__init__.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from collections import defaultdict
a = [
{
"id": 1,
"id_foreign": {
"id": 1,
"parent_id": 1
}
},
{
"id": 1,
"id_foreign": {
"id": 2,
"parent_id": 1
}
},
{
"id": 1,
"id_foreign": {
"id": 3,
"parent_id": 1
}
},
{
"id": 2,
"id_foreign": {
"id": 4,
"parent_id": 2
}
},
{
"id": 2,
"id_foreign": {
"id": 5,
"parent_id": 2
}
},
{
"id": 2,
"id_foreign": {
"id": 6,
"parent_id": 2
}
}
]
b = [
{"id": 1,
"id_foreign":[{
"id": 3,
"parent_id": 1
}, {
"id": 1,
"parent_id": 1
}, {"id": 2,
"parent_id": 1}
],
},
{"id": 2,
"id_foreign": [{
"id": 4,
"parent_id": 2
}, {
"id": 5,
"parent_id": 2
}, {
"id": 6,
"parent_id": 2}
]
}
]
tmp = defaultdict(list)
print(tmp)
from itertools import groupby
def test(*args, **kwargs):
tmp = {}
for k, v in args[0].items():
if '_foreign' not in k:
tmp[k] = v
return tmp
response_list = []
for key, group in groupby(a, test):
response = {}
for i in group:
for k , v in i.items():
if '_foreign' in k:
if k not in response:
response[k] = [v]
else:
response[k].append(v)
for response_ in response:
i.pop(response_, None)
result = i | response
response_list.append(result)
print()
# result = []
# for data_index in range(0, len(a)):
# for data_index_ in range(0, len(a)):
# foreign_ = {}
# non_foreign_ = {}
# for k, v in a[data_index].items():
# tmp = {}
# if '_foreign' in k:
# foreign_[k] = v
# else:
# non_foreign_[k] = v
#
# foreign__ = {}
# non_foreign__ = {}
# for k, v in a[data_index_].items():
# tmp = {}
# if '_foreign' in k:
# foreign__[k] = v
# else:
# non_foreign__[k] = v
# if non_foreign_ == non_foreign__:
# print(data_index)
# print(data_index_)
# print('----')
# print('===')