1 回答
TA贡献1893条经验 获得超10个赞
这种方法首先构建一个路由主控,其中包括每个路由的列表条目以进行检查(显示在底部)。
一旦路由主机可用,那么路由检查就很简单了。
flatten来自这里的 179 点答案
如果您需要任何澄清,请告诉我。
这是代码:
import _collections_abc
import itertools
from pprint import pprint
l1 = [
'1.2.3.26.4',
'1.2.3.75.32.2'
]
json_tree = {
"Gardens": {
"Seaside": {
"@loc": "porch",
"@myID": "1.2.3",
"Tid": "1",
"InfoList": {
"status": {
"@default": "0",
"@myID": "26"
},
"count": {
"@default": "0",
"@myID": "1"
}
},
"BackYard": {
"@loc": "backyard",
"@myID": "75",
"Tid": "2",
"InfoList": {
"status": {
"@default": "6",
"@myID": "32"
},
"count": {
"@default": "0",
"@myID": "2"
}
}
}
}
}
}
def flatten(d, parent_key='', sep='_'):
"""Return flattened dict as list"""
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, _collections_abc.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def add_id(path, id_value):
"""Update list of @myID paths"""
id_list.append([path, id_value])
def update_json(path, value):
"""Update the json_data with the default value"""
l_bracket_q = "['"
r_bracket_q = "']"
at_default = "['@default']"
result = ''
cmd = "json_tree"
for key in path:
cmd += l_bracket_q + key + r_bracket_q
cmd += at_default
cmd += ' = value'
exec(cmd)
flat_list = flatten(json_tree)
id_list = []
# Loop the flattened list filtering for ID's, excluding the ones in count
for k, v in [x for x in flat_list.items() if '@myID' in x[0] and 'count' not in x[0]]:
add_id(k, v)
route_list = []
# start building the route list from the filtered id list
for id_entry in id_list:
route_list.append(
[[x for x in id_entry[0].split('_') if x != '@myID'],
[y for y in id_entry[1].split('.')]])
route_master = []
# generate the route master to include the path and the full route (as list not 1.2.3)
for id_entry in id_list:
s1 = id_entry[0].split('_')
tmp_route = []
tmp_list = []
for i in range(len(s1)):
if s1[i] == '@myID':
break
tmp_list.append(s1[i])
for rte in route_list:
if tmp_list == rte[0]:
tmp_route.append(rte[1])
route_item = list(itertools.chain(*tmp_route))
tmp_list.append(route_item)
route_master.append(tmp_list)
# break out the default value from the route of the main driver file
l2 = list(zip(['.'.join(x.split('.')[:-1]) for x in l1], [x.split('.')[-1] for x in l1]))
# loop the routes to process and update when found
for route, default in l2:
for check_it in route_master:
if route.split('.') == check_it[-1]:
update_json(check_it[:-1], default)
# print results
pprint(json_tree)
结果:
{'Gardens': {'Seaside': {'@loc': 'porch',
'@myID': '1.2.3',
'BackYard': {'@loc': 'backyard',
'@myID': '75',
'InfoList': {'count': {'@default': '0',
'@myID': '2'},
'status': {'@default': '2',
'@myID': '32'}},
'Tid': '2'},
'InfoList': {'count': {'@default': '0', '@myID': '1'},
'status': {'@default': '4',
'@myID': '26'}},
'Tid': '1'}}}
路线大师:
['Gardens', 'Seaside', ['1', '2', '3']]
['Gardens', 'Seaside', 'InfoList', 'status', ['1', '2', '3', '26']]
['Gardens', 'Seaside', 'BackYard', ['1', '2', '3', '75']]
['Gardens', 'Seaside', 'BackYard', 'InfoList', 'status', ['1', '2', '3', '75', '32']]
添加回答
举报