问题描述
我所拥有的:在networkx中导入的图形G,其节点和边由gml文件加载.
What I have: a graph G imported in networkx with nodes and edges loaded by gml file.
问题:如何向选定的边E添加新属性.
Problem: How to add a new attribute to a selected edge E.
我要做什么:我想为图形的特定边E添加一个新的属性类型".注意:此边缘E不存在属性类型".
What I want to do: I want to add a new attribute 'type' for a particular edge E of my graph. Attention: the attribute 'type' doesn't exist for this edge E.
我的代码是:
G.edge[id_source][id_target]['type']= value
但是,如果我打印G的所有边缘,那么我现在有n + 1个边缘; G的所有旧边,以及新边p =(id_source,id_target,{'type'= value}).此外,旧的边缘E(我要修改的边缘)没有新的属性"type".
But if I print all the edges of G, now I have n+1 edges; all the old edges of G, and a new edge p= (id_source, id_target, {'type'= value}). Furthermore, the old edge E (the one that I want modify) doesn't have the new attribute 'type'.
所以我的代码增加了一个新的优势(我不想要).
So my code have added a new edge (that I don't want).
我要更新,添加一个不存在的新属性.
I want to update the old one adding a new attribute that doesn't exist.
推荐答案
您可能拥有networkx MultiGraph而不是图形,在这种情况下,边的属性设置有些麻烦. (您可以通过加载在节点之间具有多个边的图来获得多图).您可能通过分配属性来破坏数据结构 G.edge[id_source][id_target]['type']= value
何时需要 G.edge[id_source][id_target][key]['type']= value
.
You may have a networkx MultiGraph instead of a graph and in that case the attribute setting for edges is a little tricker. (You can get a multigraph by loading a graph with more than one edge between nodes). You may be corrupting the data structure by assigning the attribute G.edge[id_source][id_target]['type']= value
when you need G.edge[id_source][id_target][key]['type']= value
.
以下是有关图和多图的工作方式不同的示例.
Here are examples of how it works differently for Graphs and MultiGraphs.
对于Graph案例,属性的工作方式如下:
For the Graph case attributes work like this:
In [1]: import networkx as nx
In [2]: G = nx.Graph()
In [3]: G.add_edge(1,2,color='red')
In [4]: G.edges(data=True)
Out[4]: [(1, 2, {'color': 'red'})]
In [5]: G.add_edge(1,2,color='blue')
In [6]: G.edges(data=True)
Out[6]: [(1, 2, {'color': 'blue'})]
In [7]: G[1][2]
Out[7]: {'color': 'blue'}
In [8]: G[1][2]['color']='green'
In [9]: G.edges(data=True)
Out[9]: [(1, 2, {'color': 'green'})]
对于MultiGraphs,还有一个附加级别的键可以跟踪平行边缘,因此它的工作方式略有不同.如果未显式设置键,则MultiGraph.add_edge()将使用内部选择的键(顺序整数)添加一个新边.
With MultiGraphs there is an additional level of keys to keep track of the parallel edges so it works a little differently. If you don't explicitly set a key MultiGraph.add_edge() will add a new edge with an internally chosen key (sequential integers).
In [1]: import networkx as nx
In [2]: G = nx.MultiGraph()
In [3]: G.add_edge(1,2,color='red')
In [4]: G.edges(data=True)
Out[4]: [(1, 2, {'color': 'red'})]
In [5]: G.add_edge(1,2,color='blue')
In [6]: G.edges(data=True)
Out[6]: [(1, 2, {'color': 'red'}), (1, 2, {'color': 'blue'})]
In [7]: G.edges(data=True,keys=True)
Out[7]: [(1, 2, 0, {'color': 'red'}), (1, 2, 1, {'color': 'blue'})]
In [8]: G.add_edge(1,2,key=0,color='blue')
In [9]: G.edges(data=True,keys=True)
Out[9]: [(1, 2, 0, {'color': 'blue'}), (1, 2, 1, {'color': 'blue'})]
In [10]: G[1][2]
Out[10]: {0: {'color': 'blue'}, 1: {'color': 'blue'}}
In [11]: G[1][2][0]['color']='green'
In [12]: G.edges(data=True,keys=True)
Out[12]: [(1, 2, 0, {'color': 'green'}), (1, 2, 1, {'color': 'blue'})]
这篇关于如何在networkx的边缘中添加新属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!