Reputation: 105
I am trying to compare two node values with an if-statement in NetworkX. For this, my graph structure of Graph (G) and Graph (G1) looks like this:
import networkx as nx
#Define Graph G
G = nx.DiGraph()
G.add_edge('x','a', dependency=0.4)
G.add_edge('x','b', dependency=0.7)
G.add_edge('b','a', dependency=0.3)
G.add_node('x', value=10)
G.add_node('a', value=15)
G.add_node('b', value=20)
#Define Graph G1
G1 = nx.DiGraph()
G1.add_edge('x','a', dependency=0.4)
G1.add_edge('x','b', dependency=0.7)
G1.add_edge('b','a', dependency=0.3)
G1.add_node('x1', value=10)
G1.add_node('a1', value=15)
G1.add_node('b1', value=20)
Now, I simply want to compare the two node attributes of G.node(x) and G1.node(x1) and do something, if they are the same:
if G.nodes(['x']['value']) == G1.nodes['x1']['value']:
print("Both values are the same!")
But I keep getting the following error message:
TypeError: list indices must be integers or slices, not str
Can someone help me with this?
Upvotes: 2
Views: 907
Reputation: 2097
if (G.nodes['x']['value'] == G1.nodes['x1']['value']):
print("Both values are the same!")
No parenthesis when accessing nodes.
Upvotes: 3