简体   繁体   中英

networkx convert_node_labels_to_integers

Im using python 2.7 and networkx and im using convert_node_labels_to_integers. It does its main job fine. If i ask it to not discard old labels im not getting what i expected

g = nx.Graph()
g.add_edge('a','b')
g2 = nx.convert_node_labels_to_integers(g, discard_old_labels=False)
print g2[0]

g2[0] has no attributes when i thought it would now have an attribute 'old_labels' with the old label in it.

Am i doing something wrong? Am i misinterpreting how this works? Ive got to be missing something simple.

Thanks

In the latest (development, soon to be networkx-1.8) version of NetworkX this is handled differently. The old labels can optionally be stored as node atrributes

In [1]: import networkx as nx

In [2]: G = nx.Graph([('a','b'),('b','c')])

In [3]: H = nx.convert_node_labels_to_integers(G,label_attribute='old_label')

In [4]: H.node
Out[4]: {0: {'old_label': 'a'}, 1: {'old_label': 'c'}, 2: {'old_label': 'b'}}

The old labels are stored in a graph attribute - node_labels.

print g2.node_labels
{'a': 0, 'b': 1}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM