Networkx 2.0 获取邻居节点的方法

hxy    2018-11-28 09:50

NetworkX 2.1获取节点i属性集合方法:

G.node[i],返回值为dict类型。

Netowrkx中获取邻居节点的方法出在 networkx.Graph.neighbors,在早期版本中,可以直接使用G.neighbors()来获取节点的邻居节点,但是在2.0版本中做出调整,可以直接使用G[] 获取节点的邻居节点

Graph.neighbors(n)

Return an iterator over all neighbors of node n.

This is identical to iter(G[n])

Parameters:n (node) – A node in the graphReturns:neighbors – An iterator over all neighbors of node nReturn type:iteratorRaises:NetworkXError – If the node n is not in the graph.

Examples

>>>

>>> G = nx.path_graph(4)  # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> [n for n in G.neighbors(0)]
[1]

Notes

It is usually more convenient (and faster) to access the adjacency dictionary as G[n]:

>>>

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge('a', 'b', weight=7)
>>> G['a']
AtlasView({'b': {'weight': 7}})
>>> G = nx.path_graph(4)
>>> [n for n in G[0]]
[1]

 

Views: 15.9K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment