【发布时间】:2015-01-09 20:38:15
【问题描述】:
有没有更快、更好的方法来构建 Networkx 树。目前,我的代码是
for numb in range(0,len(previous)):
nodos = list(chunks(current,3))
for i in range(0,3):
G.add_edge(previous[numb],nodos[numb][i])
这通过以下方式起作用: 1. 树有 3 个分支(或边缘)。我有两个数组:
previous = [x,y,z] #These nodes have already been added to the graph
current = [xx,xy,xz, xy,yy,yz, xz,yz,zz] #This nodes need to be added.
理想情况下,我应该执行以下操作:
1. Start with x in previous:
1.1 Pick the first 3 nodes in current (i.e. xx,xy,xz)
1.1.1 Add the nodes-edges: x->xx, x->xy, x->xz
到目前为止,我的代码是这样的:
1. Start with x in previous
2. Partition current into chunks of 3 items: [[xx,xy,xz], [xy,yy,yz], [xz,yz,zz]]
3. Loop through all the nodes in these chunks:
4. Add x->xx, loop again, add x->xy... etc.
我的实现效率极低。你将如何有效地做到这一点?谢谢
【问题讨论】: