免责声明:网站内容仅供个人学习记录,禁做商业用途,转载请注明出处。

版权所有 © 2017-2020 NEUSNCP个人学习笔记 辽ICP备17017855号-2

Networkx 2.0 获取邻居节点的方法

hxy    2018年11月28日 09:50:00

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]

 

浏览: 16.9K

[[total]] 条评论

添加评论
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]楼
  2. 点击加载更多……
  3. 添加评论