簡體   English   中英

迭代節點處的邊

[英]Iterate over edges at a node

我正在嘗試裁剪圖中具有不規則值的邊,與這些邊關聯的實際值存儲在networkx之外。 我計划遍歷圖中的所有節點來進行這種修剪(100,000 個元素)。

import networkx as nx
G=nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_edges_from([(1,2),(2,3)])
edges=G.get_edges_from_node(2) #(1,2),(2,3)

我似乎無法找到此功能。

import networkx as nx
G=nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_edges_from([(1,2),(2,3)])

您可以通過以下方式遍歷所有節點:

for n in G.node.items():
    print(n)

(1, {})
(2, {})
(3, {})

您還可以將圖形外部的值添加為邊屬性並簡單地遍歷邊

G2=nx.Graph()
G2.add_node(1)
G2.add_node(2)
G2.add_node(3)
G2.add_edges_from([(1,2, {'value': 1}),(2,3, {'value': 2})])

for edge in G2.edges(data=True):
    print(edge)

(1, 2, {'value': 1})
(2, 3, {'value': 2})

如果有人來這里尋找問題標題所說的內容......

這里有一些方法可以循環遍歷節點的邊

  1. nx.Graph.edges

     G.edges([1, 2, 5]) # edges incident on nodes 1, 2 and 5 G.edges(0) # edges incident on node 0
  2. nx.Graph.adj

     G.adj[0] # neighbors adjacent to node 0
  3. 下標符號

    G[0] # neighbors adjacent to node 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM