Iterating over nodes

Hi!

I'm trying to iterate over the nodes of a graph generated from the network1.csv file. I've tried graph.nodes(), nx.nodes(graph), and graph.iter, all of which I found in the Networkx API. I get an error message saying that the graph object doesn't have any attribute 'nodes'. The same thing happens with the karate club graph, but not with a dummy graph with just one node. Do you have an idea of what this could be?

Thanks a lot.

Top comment

I'm trying to iterate over the nodes of a graph generated from the network1.csv file. I've tried graph.nodes(), nx.nodes(graph), and graph.iter, all of which I found in the Networkx API.
...
The same thing happens with the karate club graph, but not with a dummy graph with just one node.

You can take a look at the solution of lab 1 to see how to iterate over nodes and their data:

karate_club = nx.karate_club_graph()
for n in karate_club.nodes.data():
    node = n[0] # node index, e.g., 0
    club = n[1]["club"] # node club, e.g., "Mr. Hi" or "Officer"

Note that the .data() part is crucial to also retrieve the data for each node, next to its ID.

This makes use of this part of the NetworkX API: https://networkx.org/documentation/stable/reference/classes/generated/networkx.Graph.nodes.html

Make sure you have an updated version of NetworkX (2.4 or 2.5)

Top comment

I'm trying to iterate over the nodes of a graph generated from the network1.csv file. I've tried graph.nodes(), nx.nodes(graph), and graph.iter, all of which I found in the Networkx API.
...
The same thing happens with the karate club graph, but not with a dummy graph with just one node.

You can take a look at the solution of lab 1 to see how to iterate over nodes and their data:

karate_club = nx.karate_club_graph()
for n in karate_club.nodes.data():
    node = n[0] # node index, e.g., 0
    club = n[1]["club"] # node club, e.g., "Mr. Hi" or "Officer"

Note that the .data() part is crucial to also retrieve the data for each node, next to its ID.

This makes use of this part of the NetworkX API: https://networkx.org/documentation/stable/reference/classes/generated/networkx.Graph.nodes.html

Make sure you have an updated version of NetworkX (2.4 or 2.5)

Awesome, thank you!

Add comment

Post as Anonymous Dont send out notification