Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python networkx中的nx.get_node_属性返回空字典_Python_Python 3.x_Networkx - Fatal编程技术网

Python networkx中的nx.get_node_属性返回空字典

Python networkx中的nx.get_node_属性返回空字典,python,python-3.x,networkx,Python,Python 3.x,Networkx,代码如下: import networkx as nx G=nx.Graph() G.add_nodes_from([0,1,2,3,4,5]) G[0]['color']="red" G[1]['color']="yellow" G[2]['color']="red" G[3]['color']="green" G[4]['color']="green" G[5]['color']="yellow" print(nx.get_node_attributes(G,'color')) 奇怪的是,

代码如下:

import networkx as nx
G=nx.Graph()
G.add_nodes_from([0,1,2,3,4,5])
G[0]['color']="red"
G[1]['color']="yellow"
G[2]['color']="red"
G[3]['color']="green"
G[4]['color']="green"
G[5]['color']="yellow"
print(nx.get_node_attributes(G,'color'))
奇怪的是,我得到了一本空字典。有人知道原因吗?或者还有其他可能的方法吗? 关于类似问题的参考链接:

我知道下面是使用
get\u node\u attributes
方法的正确方法,但这是唯一的方法吗

>>> G=nx.Graph()
>>> G.add_nodes_from([1,2,3],color='red')
>>> color=nx.get_node_attributes(G,'color')
>>> color[1]
这是在:

节点属性

使用
Add_node()
Add_nodes_from()
G.node

>>> G.add_node(1, time='5pm')
>>> G.add_nodes_from([3], time='2pm')
>>> G.node[1]
{'time': '5pm'}
>>> G.node[1]['room'] = 714
>>> G.nodes(data=True)
[(1, {'room': 714, 'time': '5pm'}), (3, {'time': '2pm'})]

这不是一个bug;您只是没有正确设置属性。

值得一提的是
nx.set\u node\u attributes
,它允许您在定义节点后设置属性。可能重复