<p>这会将输出转换为嵌套字典形式。如果我也能找到路,我会随时通知你的。也许这个,是有帮助的。在</p>
<pre><code>list_of_tuples = [('ROOT','ROOT', 'shot'),('nsubj','shot', 'I'),('det','elephant', 'an'),('dobj','shot', 'elephant'),('case','sleep', 'in'),('nmod:poss','sleep', 'my'),('nmod','shot', 'sleep')]
nodes={}
for i in list_of_tuples:
rel,parent,child=i
nodes[child]={'Name':child,'Relationship':rel}
forest=[]
for i in list_of_tuples:
rel,parent,child=i
node=nodes[child]
if parent=='ROOT':# this should be the Root Node
forest.append(node)
else:
parent=nodes[parent]
if not 'children' in parent:
parent['children']=[]
children=parent['children']
children.append(node)
print forest
</code></pre>
<p>输出是一个嵌套字典</p>
<p><code>[{'Name': 'shot', 'Relationship': 'ROOT',
'children':
[{'Name': 'I', 'Relationship': 'nsubj'},
{'Name': 'elephant', 'Relationship':
'dobj',
'children':
[{'Name': 'an',
'Relationship': 'det'}]},
{'Name': 'sleep', 'Relationship':
'nmod',
'children':
[{'Name': 'in',
'Relationship': 'case'},
{'Name': 'my', 'Relationship':
'nmod:poss'}]}]}]</code></p>
<p>以下函数可以帮助您找到根到叶的路径:</p>
^{pr2}$