如何重命名networkx图的单个节点? [英] How to rename a single node of a networkx graph?
问题描述
我想知道如何更改有向图节点的单个节点名称.我是 networkx 的新手,只能找到有关如何更改所有节点名称的答案.
I wanted to know how I can change a single node name of a node of a digraph. I am new to networkx and could only find answers on how to change all node names.
在我的例子中,我遍历图 A 来创建图 B.p
和 c
是图 A 的节点.图的边 (p,c)A 包含我想添加到 B 的节点 p
的数据.但是,当我将图 A 的边数据添加到图 B 的现有节点 p
时,我想将 p
的名称更新为等于 c
的名称,这样我就可以为图 A 的下一条边再次引用它,因为它是边缘 (c,x) 和我可以使用 c
再次引用它...
In my case I am iterating over a graph A to create graph B. p
and c
are nodes of graph A. The edge (p,c) of graph A contains data I want to add to the node p
of B. However, when I am adding the edge data from graph A to the already existing node p
of graph B, I would like to update the name of p
to be equal to the name of c
so I am able to reference it again for the next edge of graph A because it then is the edge (c,x) and I can use the c
to reference it again...
我的代码的相关部分看起来像这样
The relevant part of my code looks like this
new_stages = A.in_edge(c, data='stages')
stages = B.node[p]['stages']
stages.append(new_stages)
<<Update node p to have name of c??>>
B.add_node(p, stages=new_stage_set)
感谢任何帮助,谢谢!
推荐答案
您有 nx.relabel_nodes
为此.这是一个简单的用例:
You have nx.relabel_nodes
for this. Here's a simple use case:
G = nx.from_edgelist([('a','b'), ('f','g')])
mapping = {'b':'c'}
G = nx.relabel_nodes(G, mapping)
G.edges()
# EdgeView([('a', 'c'), ('f', 'g')])
这篇关于如何重命名networkx图的单个节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!