我正在尝试使用以下代码在 python 中生成自定义 JSONroot={}Levels=[['L1','L1','L2'], ['L1','L1','L3'], ['L1','L2'], ['L2','L2','L3'], ['L2','L2','L1'], ['L3','L2'], ['L4','L2','L1'], ['L4','L2','L4']]def append_path(root, paths): if paths: child = root.setdefault(paths[0], {}) append_path(child, paths[1:])for p in Levels: append_path(root, p)def convert(d): templist=[] noofchildren=0 if(len(d.items())==0): return ([{}],1) for k,v in d.items(): temp,children=convert(v) noofchildren+=children if(temp): templist.append({"name":k+"("+str(children)+")",'children':temp}) else: templist.append({'name': k+"("+str(children)+")", 'children':[{}]}) return (templist,noofchildren) # Print resultsimport jsonprint(json.dumps(convert(root)[0], indent=2))我的数据集发生了一些变化 Levels=[[['L1','L1','L2'],[10,20,30]], [[['L1','L1','L3'],[10,15,20]], [[['L1','L2'],[20,10]], [[['L2','L2','L3'],[20,20,30]], [[['L2','L2','L1'],[10,20,30]] [[['L3','L2'],[10,20]] [[['L4','L2','L1'],[10,20,10]] [[['L4','L2','L4'],[20,40,50]]]如何更改我的代码以添加此信息?
添加回答
举报
0/150
提交
取消