Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python and networkX keyerror

Tags:

python

i have this problem in python, python keeps giving me an keyerror: weight

  g.add_edge(1,3,weight=2.5)
  g[1][2]['weight'] = 1.5
  for n1,n2,attr in g.edges(data=True):
    print n1,n2,attr['weight']

The output.

    KeyError                                  Traceback (most recent call last)
    <ipython-input-56-832c29e7e1db> in <module>()
         2 g[1][2]['weight'] = 1.5
         3 for n1,n2,attr in g.edges(data=True):
   ----> 4      print n1,n2,attr['weight']

   KeyError: 'weight'

   0 1

i dont know why weight gives me an error?

like image 470
Alexander Marcussen Avatar asked Oct 18 '25 10:10

Alexander Marcussen


1 Answers

My guess is that you have some other edges in your graph, and haven't set the weight attribute for all of them. Try the following:

for n1,n2,attr in g.edges(data=True):
    print n1,n2,attr

See if attr contains a value for weight in every case.

like image 116
bgschiller Avatar answered Oct 20 '25 23:10

bgschiller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!