networkx.relabel.relabel_nodes(G, mapping, copy=True)
上記mapping
部分に、古いノードをキーとし新しいノードを値とする辞書を指定することによって、ノードの入れ替えが可能です。
デフォルトでは入れ替え後のグラフのコピーを返します。G
で指定したグラフそのものを変更したい場合は、変数G
に代入し直すか、copy
キーワードをFalse
とします。
python
1>>> import networkx as nx
2>>> tG = nx.DiGraph()
3>>> tG.add_nodes_from([(2,0), (4,3)])
4>>> tG.add_edges_from([((2,0), (4,3))])
5>>> tG.number_of_nodes()
62
7>>> list(tG.nodes)
8[(2, 0), (4, 3)]
9>>> tG.number_of_edges()
101
11>>> list(tG.edges)
12[((2, 0), (4, 3))]
13>>> nx.relabel_nodes(tG, {(4,3): (4,10)}, copy=False)
14<networkx.classes.digraph.DiGraph object at 0x7f81081357c0>
15>>> tG.number_of_nodes()
162
17>>> list(tG.nodes)
18[(2, 0), (4, 10)]
19>>> tG.number_of_edges()
201
21>>> list(tG.edges)
22[((2, 0), (4, 10))]
https://networkx.org/documentation/stable/reference/generated/networkx.relabel.relabel_nodes.html
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。