You can directly assign weights using G [u] [v] ['weight'], for example
In [1]: import networkx as nx In [2]: import random In [3]: G = nx.path_graph(10) In [4]: for u,v in G.edges(): ...: G[u][v]['weight'] = random.paretovariate(2) ...: ...: In [5]: print G.edges(data=True) [(0, 1, {'weight': 1.6988521989583232}), (1, 2, {'weight': 1.0749963615177736}), (2, 3, {'weight': 1.1503859779558812}), (3, 4, {'weight': 1.675436575683888}), (4, 5, {'weight': 1.1948608572552846}), (5, 6, {'weight': 1.080152340891444}), (6, 7, {'weight': 1.0296667672332183}), (7, 8, {'weight': 2.0014384064255446}), (8, 9, {'weight': 2.2691612212058447})]
I used Python random.paretovariate () to select the weight, but of course you can place whatever you want.
source share