向networkx中的边添加新属性 [英] Add new attribute to an edge in networkx

查看:394
本文介绍了向networkx中的边添加新属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么:在networkx whit节点导入的图G,以及由gml文件加载的egdes。

问题:如何向所选边缘添加新属性E.

我想要做什么:我想要为我的图形的特定边E添加新的属性类型。注意:这个边缘E不存在属性'type'。



我读了很多互联网和这里提出的解决方案,但是这些解决方案都没有解决我的问题。
其实我的代码是:

pre $ g $edge.gif [id_source] [id_target] ['type'] = value

但是,如果我打印G的所有边,现在我有n + 1个边,所有旧的G的边,以及新的边p =(id_source,id_target,{'type'= value})。此外,旧边E(我想修改的那个)没有新的属性'type'。

所以我的代码添加了一个新的边我不想要)。

我希望更新旧版本添加一个不存在的新属性。



感谢您的帮助!
$ b

编辑求助
感谢Aric和一些技巧我解决了我的问题:

$ p $ def add_attribute_to_edge(H,id_node_source,id_node_target,new_attr,value_attr):

keydict = H [id_node_source] [id_node_target]
key = len(keydict)
用于keydict中的k:
如果'type'不在hedge中[id_source] [id_target] [k ]:
H.add_edge(id_node_source,id_node_target,key = k,new_attr = value_attr)


解决方案

您可能有一个networkx MultiGraph而不是一个图形,在这种情况下边缘的属性设置是一个小值le诀窍。 (您可以通过加载一个节点之间具有多条边的图来获得多图)。当您需要$ b $时,您可能正在通过分配属性
G.edge [id_source] [id_target] ['type'] = value 来破坏数据结构b G.edge [id_source] [id_target] [key] ['type'] = value



是Graphs和MultiGraphs工作方式的例子。



对于图表大小写属性可以这样工作:

  In [ 1]:import networkx as nx 

In [2]:G = nx.Graph()

In [3]:G.add_edge(1,2,color = 'red')

在[4]中:G.edges(data = True)
Out [4]:[(1,2,{'color':'red'}) ]

在[5]中:G.add_edge(1,2,color ='blue')

在[6]中:G.edges(data = True)$ b [b] [b] [6]:[(1,2,{'color':'blue'})]

In [7]:G [1] [2]
Out [7]:{'color':'blue'}

在[8]中:G [1] [2] ['color'] ='green'

在[9]中:G.edges(data = True)
Out [9]:[(1,2,{'color':'green'})]

在MultiGraph中,有一个额外级别的按键用于跟踪并行边缘,因此它的工作原理有所不同。如果你没有明确地设置一个键MultiGraph.add_edge()将添加一个新的边与一个内部选择的键(顺序整数)。

 在[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')

在[6]中:G.edges(data = True)
Out [6]:[(1,2,{'color':'red'}) ,(1,2,{'color':'blue'})]

在[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')

在[9]中:G.edges(data = True,keys = True)
Out [ (1,2,1,{'color':'blue'})]

[ 10]:G [1] [2]
Out [10]:{0:{'color':'blue'},1:{'color':'blue'}}

In [11]:G [1] [2] [0] ['color'] ='green'

在[12]中:G.edges(data = True,keys = True )
出[12]:[(1,2,0,{'color':'green'}) ,(1,2,1,{'color':'blue'})]


What i Have: a graph G imported in networkx whit nodes and egdes loaded by gml file.
Problem : How to add a new attribute to a selected edge 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.

I read a lot of solutions proposed in Internet and here, but no one of those solutions solves my problem. In fact my code is:

  G.edge[id_source][id_target]['type']= value

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 update the old one adding a new attribute that doesn't exist.

Thank you for your help !

EDIT: SOLVED Thanks to Aric and some tricks i solved my problem:

def add_attribute_to_edge(H,id_node_source,id_node_target,new_attr,value_attr):

      keydict =H[id_node_source][id_node_target]
      key=len(keydict)
      for k in keydict:
          if 'type' not in H.edge[id_source][id_target][k]:
             H.add_edge(id_node_source,id_node_target,key=k, new_attr= value_attr)

解决方案

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.

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'})]

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中的边添加新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆