将图中的所有节点重命名为数字序列
我正在使用 Python 的 NetworkX 图形库。在我的程序中的某个时刻,我想将我的nodeID“合并”为一系列数字。这是我的天真的方法:
start = 1 # could be anything
for i, n in enumerate(g.nodes()):
if i+start == n:
continue
g.add_node(i+start, attr_dict=g.node[n])
g.add_edges_from([(i+start, v, g[n][v]) for v in g.neighbors(n)])
g.remove_node(n)
有没有比所有邻居的详尽副本更快的方法?例如,我尝试了 g[i+start] = g[n]
,但这是禁止的。
谢谢!
I am using the NetworkX graph library for Python. At some point in my program I would like to "consolidate" my nodeIDs into a sequence of numbers. Here's my naive approach:
start = 1 # could be anything
for i, n in enumerate(g.nodes()):
if i+start == n:
continue
g.add_node(i+start, attr_dict=g.node[n])
g.add_edges_from([(i+start, v, g[n][v]) for v in g.neighbors(n)])
g.remove_node(n)
Is there a faster way than this exhaustive copy of all the neighbors? For example, I tried g[i+start] = g[n]
, but that is forbidden.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这行得通吗?
http://networkx.github.io/documentation/latest /reference/ generated/networkx.relabel.convert_node_labels_to_integers.html
返回:
现在:
返回:
Would this work?
http://networkx.github.io/documentation/latest/reference/generated/networkx.relabel.convert_node_labels_to_integers.html
returns:
now:
returns:
如果您仍然感兴趣,可以查看
networkx.relabel_nodes()
它采用映射字典。In case your interest is still relevant, there is
networkx.relabel_nodes()
which takes a mapping dictionary.