繁体   English   中英

联合igraph-python的多个Graph对象,包括属性

[英]Unite several Graph objects of igraph-python including attributes

我有几个子图,我想重新组合成一个图,目前我将它们保存在字典中,例如:

In [364]: result
Out[364]: 
{0: <igraph.Graph at 0x7f5b0f684de0>,
 1: <igraph.Graph at 0x7f5b0f684af8>,
 2: <igraph.Graph at 0x7f5b0f517050>,
 3: <igraph.Graph at 0x7f5b0f517148>,
 4: <igraph.Graph at 0x7f5b0f517240>}

这些子图中的每个子图都有一个属性ind

In [367]: result[1].vs['name']
Out[367]: ['633', '634', '971']

In [368]: result[2].vs['name']
Out[368]: ['637']

但是,当我尝试将它们组合成一个igraph.Graph对象时,似乎它们失去了ind属性:

G = igraph.Graph()
G+=result[0]
G+=result[1]
G+=result[2]
G.vs["name"]
Traceback (most recent call last):

  File "<ipython-input-370-72297b64297b>", line 1, in <module>
    G.vs["name"]

KeyError: 'Attribute does not exist'

我在这里做错了什么?

这是我正在尝试做的草图:

import igraph
sub1 = igraph.Graph.Full(3)
sub1.vs["name"] = ["1", "2", "3"]
sub2 = igraph.Graph.Full(2)
sub2.vs["name"] = ["4", "5"]
result = [sub1,sub2]
G = igraph.Graph()
G += result[0]
G += result[1]
G.vs["name"]
Traceback (most recent call last):

  File "<ipython-input-15-bc406e721319>", line 1, in <module>
    G.vs["name"]

KeyError: 'Attribute does not exist'

您在这里没有做错任何事; 不幸的是,当将图形添加到另一个图形时,Python接口不支持保留顶点属性。 您必须分别添加属性:

G = Graph()
G += result[0]
G += result[1]
G += result[2]
G.vs["ind"] = sum((subgraph.vs["ind"] for subgraph in result), [])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM