evansyangs evansyangs
首页
分类
标签
归档
GitHub (opens new window)

Evan Yang

摸鱼小能手
首页
分类
标签
归档
GitHub (opens new window)
  • NetworkX Learning Notes
  • 1. Creating a graph
  • 2. Nodes
  • 3. Edges
  • 4. What to use as nodes and edges
  • 5. Accessing edges and neighbors
  • 6. Adding attributes to graphs, nodes, and edges
  • 7. Analyzing graphs
  • 8. Directed graphs
  • 9. Drawing graphs
  • 10. Graph generators and graph operations
  • 11. Multigraphs

5. Accessing edges and neighbors

In addition to the views Graph.edges() (opens new window), and Graph.adj() (opens new window), access to edges and neighbors is possible using subscript notation.

G[1]  # same as G.adj[1]

G[1][2]

G.edges[1, 2]
1
2
3
4
5

You can get/set the attributes of an edge using subscript notation if the edge already exists.

G.add_edge(1, 3)
G[1][3]['color'] = "blue"
G.edges[1, 2]['color'] = "red"
1
2
3

Fast examination of all (node, adjacency) pairs is achieved using G.adjacency(), or G.adj.items().

提示

Note that for undirected graphs, adjacency iteration sees each edge twice.

FG = nx.Graph()
FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)])
for n, nbrs in FG.adj.items():
   for nbr, eattr in nbrs.items():
       wt = eattr['weight']
       if wt < 0.5: print('(%d, %d, %.3f)' % (n, nbr, wt))
1
2
3
4
5
6

Convenient access to all edges is achieved with the edges property.

for (u, v, wt) in FG.edges.data('weight'):
    if wt < 0.5: print('(%d, %d, %.3f)' % (u, v, wt))
1
2
编辑 (opens new window)
#Python
上次更新: 2021/02/16, 02:45:37
4. What to use as nodes and edges
6. Adding attributes to graphs, nodes, and edges

← 4. What to use as nodes and edges 6. Adding attributes to graphs, nodes, and edges→

最近更新
01
Dell7472黑苹果
03-27
02
上手HackMD
04-13
03
Windows Terminal设置
03-14
更多文章>
Theme by Vdoing | Copyright © 2019-2023 Evan Yang
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式