我有一个numpy数组,我想为数组中的每个点创建顶点。我使用networkx作为图形支持方法(文档如下: http://networkx.github.io/documentation/latest/tutorial/)
我想把数组中的每个元素当作一个像素位置,并在每个位置创建一个顶点实例。这很容易使用一个简单的for循环:
new=np.arange(16)
gnew=nx.Graph()
for x in new:
if new[x]>0:
gnew.add_node(x)
h=gnew.number_of_nodes()
print h
如预期,将打印15个节点。但是,当您有相同的值时,这会变得更加棘手。例如:
^{pr2}$现在,由于所有值都相同-(1),因此只有一个节点将添加到图形中。有没有办法绕过这个问题?在
NetworkX要求每个节点都有一个唯一的名称。您可以生成唯一的名称,然后将数组的元素设置为节点的属性,例如
相关问题 更多 >
编程相关推荐