i am using the edges_iter function in networkx python…i wish to randomly extract edges from a graph and then using these edges, get the nodes at each end… i heard that edge_iter could help me with getting the random edges (i need random edges) but i cant figure out how to get the nodes attached. i’ve saved the graph in gpickle format and load it…then i use
a=G.edges_iter()
print a
and i get this
indicating the occurance of an edge…what i want next is to find out the nodes that are attached at the ends of this edge…help me out with this…thanks a lot guys.
i am using the edges_iter function in networkx python…i wish to randomly extract edges
Share
edges_iterreturns a generator object, so I guess you want to iterate through the edges that you get.Anyway, the edges already contain information about the nodes they are associated with, since each edge can be seen as a tuple of two nodes. Consider this example:
edge[0]will be one node andedge[1]will be the second.However, I don’t see how you would get random edges with this approach. Another solution to do that could be to use
random.choiceand just select edges with that:If you instead want to select a number of random edges, you could use
random.sample: