[英]MULTI level dict: Return reference instead of the content
I have a tree.我有一棵树。 I have to calculate cummulatve sum up to every leaf.
我必须计算每片叶子的累计总和。 And finally keep only the top N leafs by sum.
最后按总和只保留前 N 个叶子。
So far I got the sum and i get the leafs content back.. the problem is I want back REFERENCES to the leafs so that I delete the ones with low cumsum到目前为止,我得到了总和,我得到了叶子的内容。问题是我想要回到叶子的参考文献,这样我就删除了那些 cumsum 低的
Is there way to do that?有办法吗?
import numpy as np
class BeamTree(dict):
def __init__(self, *args, **kwargs):
super(BeamTree, self).__init__(*args, **kwargs)
self.__dict__ = self
self.nodes = []
def add(self, a, aa, q):
self.nodes.append( BeamTree({ 'a': a, 'aa': aa, 'qq': q, 'qs':0 }) )
return self.nodes[-1]
def qsum(self, q=0):
if len(self.nodes) == 0 : return []
leafs = []
for node in self.nodes:
node['qs'] = q + node['qq']
leafs.extend( node.qsum(node['qs']) )
if len(node.nodes) == 0 : leafs.append(node)
if len(leafs) > 0 : return leafs
return []
def generate(self, branch=3, depth=3):
if depth < 1 : return
for b in range(branch) :
sym = 's' + str(np.random.randint(100))
aix = np.random.randint(100)
q = np.random.rand()
node = self.add(sym, aix, q)
node.generate(branch, depth-1)
here is a test:这是一个测试:
In [212]: b=BeamTree(); b.generate(2,2)
In [213]: l=b.qsum(0)
In [214]: b
Out[214]:
{'nodes': [{'a': 's80',
'aa': 56,
'qq': 0.673,
'qs': 0.673,
'nodes': [{'a': 's8', 'aa': 16, 'qq': 0.115, 'qs': 0.788, 'nodes': []}, {'a': 's64', 'aa': 10, 'qq': 0.599, 'qs': 1.272, 'nodes': []}]},
{'a': 's67',
'aa': 0,
'qq': 0.900,
'qs': 0.900,
'nodes': [{'a': 's69', 'aa': 23, 'qq': 0.801, 'qs': 1.700, 'nodes': []}, {'a': 's8', 'aa': 41, 'qq': 0.826, 'qs': 1.726, 'nodes': []}]}]}
In [215]: l
Out[215]:
[{'a': 's8', 'aa': 16, 'qq': 0.115, 'qs': 0.788, 'nodes': []},
{'a': 's64', 'aa': 10, 'qq': 0.599, 'qs': 1.272, 'nodes': []},
{'a': 's69', 'aa': 23, 'qq': 0.801, 'qs': 1.700, 'nodes': []},
{'a': 's8', 'aa': 41, 'qq': 0.826, 'qs': 1.726, 'nodes': []}]
In [216]: del l[0]
In [217]: l
Out[217]:
[{'a': 's64', 'aa': 10, 'qq': 0.599, 'qs': 1.272, 'nodes': []},
{'a': 's69', 'aa': 23, 'qq': 0.801, 'qs': 1.700, 'nodes': []},
{'a': 's8', 'aa': 41, 'qq': 0.826, 'qs': 1.726, 'nodes': []}]
In [218]: b
Out[218]:
{'nodes': [{'a': 's80',
'aa': 56,
'qq': 0.673,
'qs': 0.673,
'nodes': [{'a': 's8', 'aa': 16, 'qq': 0.115, 'qs': 0.788, 'nodes': []}, {'a': 's64', 'aa': 10, 'qq': 0.599, 'qs': 1.272, 'nodes': []}]},
{'a': 's67',
'aa': 0,
'qq': 0.900,
'qs': 0.900,
'nodes': [{'a': 's69', 'aa': 23, 'qq': 0.801, 'qs': 1.700, 'nodes': []}, {'a': 's8', 'aa': 41, 'qq': 0.826, 'qs': 1.726, 'nodes': []}]}]}
In [219]:
I want back REFERENCES to the leafs so that I delete the ones with low cumsum
我想将 REFERENCES 返回到叶子,以便我删除那些 cumsum 低的
Actually, you got references to the leaves, as in Python "everything" is a reference, but what you really need is the reference to the parent's nodes
list, so that you can remove the leaf reference from that list .实际上,您得到了对叶子的引用,如 Python 中的“一切”都是一个引用,但您真正需要的是对父
nodes
列表的引用,以便您可以从该列表中删除叶子引用。
So something like this:所以像这样:
if len(node.nodes) == 0: leafs.append((self.nodes, node))
Now you have (list, node) tuples, and to remove such leaf, you would do something like this:现在你有了 (list, node) 元组,要删除这样的叶子,你会做这样的事情:
for nodes, leaf in leafs:
if <your condition for removal>:
nodes.remove(leaf)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.