如何通过图访问作为图节点的 class 实例的属性?
How to access attributes of a class instance which is a graph node via the graph?
Class定义:
class Blah:
def __init__(self,x):
self.x = x
main()部分:(导入networkx)
G = networkx.Graph()
H = []
for i in range(1,5):
H.append(Blah(i))
for i in H:
G.add_node(i)
现在,如果我想使用 G 打印 H[2].x
,我该怎么做?
G[2].x
肯定不行。 G(H[2]).x
行得通吗?
只是询问信息。我可以在我的问题中使用 H。
所以如果你定义:
class Blah:
def __init__(self,x):
self.x = x
G = networkx.Graph()
H = []
for i in range(1,5):
H.append(Blah(i))
for i in H:
G.add_node(i)
现在你想访问G的节点属性,为此你需要使用networkx命令访问G的节点:
for node in G:
print(node.x)
> 1
> 2
> 3
> 4
您可以访问如下所示的节点数据
print(list(G.nodes())[1].x)
Class定义:
class Blah:
def __init__(self,x):
self.x = x
main()部分:(导入networkx)
G = networkx.Graph()
H = []
for i in range(1,5):
H.append(Blah(i))
for i in H:
G.add_node(i)
现在,如果我想使用 G 打印 H[2].x
,我该怎么做?
G[2].x
肯定不行。 G(H[2]).x
行得通吗?
只是询问信息。我可以在我的问题中使用 H。
所以如果你定义:
class Blah:
def __init__(self,x):
self.x = x
G = networkx.Graph()
H = []
for i in range(1,5):
H.append(Blah(i))
for i in H:
G.add_node(i)
现在你想访问G的节点属性,为此你需要使用networkx命令访问G的节点:
for node in G:
print(node.x)
> 1
> 2
> 3
> 4
您可以访问如下所示的节点数据
print(list(G.nodes())[1].x)