DiGraph.has_edge#

DiGraph.has_edge(u, v)#

如果边 (u, v) 在图中,则返回 True。

这与 v in G[u] 相同,但不会抛出 KeyError 异常。

参数:
u, v节点

节点可以是字符串或数字等。节点必须是可哈希的(且非 None)Python 对象。

返回:
edge_ind布尔值

如果边在图中则为 True,否则为 False。

示例

>>> G = nx.path_graph(4)  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.has_edge(0, 1)  # using two nodes
True
>>> e = (0, 1)
>>> G.has_edge(*e)  #  e is a 2-tuple (u, v)
True
>>> e = (0, 1, {"weight": 7})
>>> G.has_edge(*e[:2])  # e is a 3-tuple (u, v, data_dictionary)
True

以下语法等价

>>> G.has_edge(0, 1)
True
>>> 1 in G[0]  # though this gives KeyError if 0 not in G
True