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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With