Get all edges linked to given node with data in networkx graph

Viewed 1816

I have a networkx graph g that has edges with weights, and I want to get all the edges along with their weights that are linked to a given node, say node 1. I know that I can do something like g.edges(1), however this will only get the edges, but not actually their associated weight attributes.

In effect, I want to do something like g.edges().data() by only for edges linked to a give node, something like g.edges(1).data(), but that doesn't work. Is there a workaround for this, or do I have to do a manual search over all edges?

1 Answers

You can use Graph.adj to do that.

try the following example:

g.adj[1] # should return information including the weights

update:

To get the data when running .edges(), you can just pass the data as a key-word argument instead.

FG.edges(1, data="weight")
Related