Python 字典树:压缩非分支(/串行)段时 .copy() 出现问题
当树枝不分枝时,我应该如何压缩它们?
这是我的字典:
mydict = {
0: {'c': [], 'p': [1]}, 1: {'c': [0], 'p': [2]}, 2: {'c': [1], 'p': [3]}, 3: {'c': [2], 'p': [7]},
4: {'c': [], 'p': [5]}, 5: {'c': [4], 'p': [6]}, 6: {'c': [5], 'p': [7]},
7: {'c': [3,6], 'p': [8]}, 8: {'c': [7], 'p': [9]}, 9: {'c': [8], 'p':[11]}
10: {'c': [], 'p': [11]}, 11: {'c': [9,10],'p': []}}
如您所见,所有键 [0,1,2,3] 属于同一分支并以单个叶子结尾。对于我的项目,我想将它们分组,这样我的最终字典看起来像:
dict2 = {
0: {'c': [], 'p': [7], 'via':[1,2,3]}, 1: {'see':0}, 2: {'see':0}, 3: {'see':0},
4: {'c': [], 'p': [7], 'via':[5,6] }, 5: {'see':4}, 6: {'see':4},
7: {'c': [3,6], 'p': [11], 'via':[8,9] }, 8: {'see':7}, 9: {'see':7},
10: {'c': [], 'p': [11]} 'via':[] },
11: {'c': [9,10],'p': []} 'via':[] }}
我发现它不是制作一本新字典,而是弄乱了我的输入字典,并且研究表明即使使用 .copy ,这也是正常的()
。我认为使用 copy.deepcopy() 会使代码难以阅读。另一方面,Pandas 也不适合,因为将值附加到单元格(即操作单元格内的列表)很麻烦。
所以现在我想问如何用干净的Python代码来完成这个?
(附上我的尝试:)
def tree_aggregate(mydict):
tree0 = mydict.copy()
for key, value in list(tree0.items()):
if len(value['c']) == 1:
child = value['c'][0]
if 'via' in tree0[child]:
tree0[child].append(tree0[child]['p'])
else:
tree0[child]['via'] = value['p'].copy()
tree0[child]['p'] = value['p']
...
return tree0
etc...
请注意, tree0[key]= {'see':child}
尚未实现,因为这需要我创建一个函数:
def fetch_see(b, dic1):
print (b)
if 'see' in b:
fetch_see(dic1[b['see']],dic1)
else:
return b['c'][0]
How should I compress tree branches when they do not branch?
This is my dictionary:
mydict = {
0: {'c': [], 'p': [1]}, 1: {'c': [0], 'p': [2]}, 2: {'c': [1], 'p': [3]}, 3: {'c': [2], 'p': [7]},
4: {'c': [], 'p': [5]}, 5: {'c': [4], 'p': [6]}, 6: {'c': [5], 'p': [7]},
7: {'c': [3,6], 'p': [8]}, 8: {'c': [7], 'p': [9]}, 9: {'c': [8], 'p':[11]}
10: {'c': [], 'p': [11]}, 11: {'c': [9,10],'p': []}}
As you can see, all keys [0,1,2,3] belong to the same branch and end at a single leaf. For my project I want to group them, such that my final dictionary looks like:
dict2 = {
0: {'c': [], 'p': [7], 'via':[1,2,3]}, 1: {'see':0}, 2: {'see':0}, 3: {'see':0},
4: {'c': [], 'p': [7], 'via':[5,6] }, 5: {'see':4}, 6: {'see':4},
7: {'c': [3,6], 'p': [11], 'via':[8,9] }, 8: {'see':7}, 9: {'see':7},
10: {'c': [], 'p': [11]} 'via':[] },
11: {'c': [9,10],'p': []} 'via':[] }}
I found that rather than making a new dictionary, it messed up my input-dictionary and recherches showed that this is a normal even when using .copy()
. I think using copy.deepcopy()
makes the code difficult to read. Pandas on the other hand is not suitable either since it is cumbersome to append values to cells, i.e. manipulating lists inside cells.
So now I want to ask how this would be done with clean python code?
(My attempt attached:)
def tree_aggregate(mydict):
tree0 = mydict.copy()
for key, value in list(tree0.items()):
if len(value['c']) == 1:
child = value['c'][0]
if 'via' in tree0[child]:
tree0[child].append(tree0[child]['p'])
else:
tree0[child]['via'] = value['p'].copy()
tree0[child]['p'] = value['p']
...
return tree0
etc...
Note that the tree0[key]= {'see':child}
is not yet implemented since this required me to make a function:
def fetch_see(b, dic1):
print (b)
if 'see' in b:
fetch_see(dic1[b['see']],dic1)
else:
return b['c'][0]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,所以我解决了它,但如果你认为它看起来很糟糕,我喜欢建设性的批评:
Yes so I solved it but if you think it looks bad I love constructive critique: