Python NetworkX:如何查找定向边的源节点和目标节点

Python NetworkX:如何查找定向边的源节点和目标节点,python,graph,nodes,networkx,edges,Python,Graph,Nodes,Networkx,Edges,同上。。我在NetworkX文档中找不到任何内容 在Python Igraph中,我可以使用: import igraph as ig G = ig.Graph(directed=True) G.add_vertices(2) G.add_edge(0,1) eid = G.get_eid(0,1) edge = G.es[eid] nodes = (edge.source, edge.target) print nodes 只需使用g.edges() 添加节点和边,按您希望的方式添加,并在有

同上。。我在NetworkX文档中找不到任何内容

在Python Igraph中,我可以使用:

import igraph as ig
G = ig.Graph(directed=True)
G.add_vertices(2)
G.add_edge(0,1)
eid = G.get_eid(0,1)
edge = G.es[eid]
nodes = (edge.source, edge.target)
print nodes
只需使用g.edges()

添加节点和边,按您希望的方式添加,并在有节点和边时添加

g.edges() 
将返回包含相邻节点的元组列表。

只需使用g.edges()

添加节点和边,按您希望的方式添加,并在有节点和边时添加

g.edges() 

将返回包含相邻节点的元组列表。

对于networkx,边只是具有两个节点实例的元组:

g = networkx.DiGraph()
g.add_edge(1,2)
edge1 = networkx.edges(g)[0]
print type(edge1), edge1
print "source:", edge1[0]
print "target:", edge1[1]
print g.neighbors(edge1[0])

对于networkx,边只是具有两个节点实例的元组:

g = networkx.DiGraph()
g.add_edge(1,2)
edge1 = networkx.edges(g)[0]
print type(edge1), edge1
print "source:", edge1[0]
print "target:", edge1[1]
print g.neighbors(edge1[0])

元组的顺序很重要。第一个元素是源元素,第二个元素是目标元素

In [1]: import networkx as nx

In [2]: G = nx.DiGraph()

In [3]: G.add_edge(1,2) # 1->2

In [4]: G.add_edge(2,3) # 2->3

In [5]: list(G.edges())
Out[5]: [(1, 2), (2, 3)] # 1->2 and 2->3

In [6]: G.add_edge(42,17) # 42->17

In [7]: list(G.edges())
Out[7]: [(1, 2), (2, 3), (42, 17)]

In [8]: for e in G.edges():
   ...:     source,target = e
   ...:     print source
   ...:     
1
2
42

元组的顺序很重要。第一个元素是源元素,第二个元素是目标元素

In [1]: import networkx as nx

In [2]: G = nx.DiGraph()

In [3]: G.add_edge(1,2) # 1->2

In [4]: G.add_edge(2,3) # 2->3

In [5]: list(G.edges())
Out[5]: [(1, 2), (2, 3)] # 1->2 and 2->3

In [6]: G.add_edge(42,17) # 42->17

In [7]: list(G.edges())
Out[7]: [(1, 2), (2, 3), (42, 17)]

In [8]: for e in G.edges():
   ...:     source,target = e
   ...:     print source
   ...:     
1
2
42

如果你能提供更多关于你想要什么的信息,那就更容易回答你的问题了。你的出发点是什么?定向边?@Joel:请参见上面的编辑;-)如果你能提供更多关于你想要什么的信息,那就更容易回答你的问题了。你的出发点是什么?定向边?@Joel:请参见上面的编辑;-)这不回答我的问题这不回答我的问题这不回答我的问题question@sal:那么你可能应该更具体一点你到底想知道什么!这确实回答了问题。你问过如何找到源边和目标边的列表这并没有回答我的问题question@sal:那么你可能应该更具体一点你到底想知道什么!这确实回答了问题。您询问了如何查找源边和目标边的列表