How to change the id value of a node in a GML file

1k Views Asked by At

How do I create a new node in a nxGraph with either a custom ID, or relabel the ID? The property I'm trying to change is that id label that's set to '0':

graph [
  node [
    id 0
    label "Category:Class-based_Programming_Languages"
  ]

I've tried to do this but it didn't work:

G = nx.Graph()
pageid = 12345
G.add_node('test', id = pageid)

But this does not change the 'id' value, rather, it is just straight-up ignored. The changed id can be seen right there on the Python program, but the problem lies with using write_gml function. It does not change that id value. Does anyone know how I can go about this? Thank you!

1

There are 1 best solutions below

3
On

Node attributes can be set in the way that you are trying, but if you try to access them with the "compact" node accessor, they are not shown. Various ways are shown below:

import networkx as nx

G = nx.Graph()
pageid = 12345
G.add_node('test', id = pageid)

# the basic node iterator doesn't show the attributes:    
print G.nodes()
>>> ['test']

# but here are some ways to access them:
print G.nodes(data=True)
>>> [('test', {'id': 12345})]

print nx.get_node_attributes(G, "id")
>>> {'test': 12345}

print G.node['test']
>>> {'id': 12345}