将节点等级添加到networkx中的节点标签

时间:2019-09-21 21:14:40

标签: python attributes nodes networkx

我想将每个节点的度数添加到该节点的标签中。如果foo的等级为1,我希望标签为foo-1。我已经尝试过nx.set_node_attributes(G, G.degree(), 'label'),但这并不会改变标签,即使我猜想它会代替标签而不是增加度数。

1 个答案:

答案 0 :(得分:0)

我设法在nx.relabel_nodes循环中使用for解决了这个问题。它不是很漂亮,但是可以。

    for node in G.nodes():
    d = G.degree(node)
    mapping = {node: node + ' - ' + str(d)}
    G = nx.relabel_nodes(G, mapping)