Nodes and attributes

km and city size.

Nodes and edges can carry attributes: km, size.

Goal

Set city size and print it back.

import networkx as nx
G = nx.Graph()
G.add_node('Nairobi', size=4.4)
G.add_node('Kisumu', size=0.6)
print(G.nodes['Nairobi']['size'])
import networkx as nx
G = nx.Graph()
G.add_edge('Nairobi', 'Nakuru', km=160)
print(G['Nairobi']['Nakuru']['km'])
import networkx as nx
G = nx.Graph()
G.add_nodes_from([('Nairobi', {'pop': 4.4}), ('Mombasa', {'pop': 1.2})])
print(dict(G.nodes(data=True)))
import networkx as nx
G = nx.Graph()
G.add_edge('a', 'b', km=10)
print(nx.get_edge_attributes(G, 'km'))