【问题标题】:Iterate over edges at a node迭代节点处的边
【发布时间】:2016-05-26 01:29:31
【问题描述】:

我正在尝试裁剪图形中具有不规则值的边,与这些边相关的实际值存储在 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)

我似乎找不到这个功能。

【问题讨论】:

  • 不规则值是什么意思?
  • @RafaelCardoso 它是我的问题特有的,在我的情况下,边缘保存值(networkx 之外)。因此,如果通向节点的边的值看起来像[1,2,3,999],那么我将修剪最后一个连接。
  • 你能说明样本输入和样本输出吗?也许您只是在寻找G.edges(2),但不清楚。
  • 所以您是说您正在使用加权图?看看这个例子:networkx.github.io/documentation/networkx-1.10/examples/drawing/…

标签: python graph networkx


【解决方案1】:
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})

【讨论】:

    【解决方案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
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多