联合 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), [])
我有几个子图想合并成一个图,目前我将它们保存在字典中,例如:
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), [])