我一直在使用networkx,但是我正在尝试添加新属性来使用
nx.set_node_attributes(G, 'Pi', PiDict)
我得到:
File "/usr/local/lib/python3.5/dist-packages/networkx/classes/function.py", line 656, in set_node_attributes
G.nodes[n][name] = values
TypeError: unhashable type: 'dict'
G是:
nx.info(G)
Out[5]: 'Name: \nType: DiGraph\nNumber of nodes: 20\nNumber of edges: 72\nAverage in degree: 3.6000\nAverage out degree: 3.6000'
PiDict是字典:
{0: 0.12500000001440292,
1: 0.03125000000360126,
2: 0.03125000000360079,
3: 0.03125000000360083,
4: 0.03125000000360079,
5: 0.12499999999611916,
6: 0.031249999999029825,
7: 0.031249999999029828,
8: 0.031249999999029828,
9: 0.03124999999902983,
10: 0.12499999999082015,
11: 0.03124999999770508,
12: 0.03124999999770508,
13: 0.03124999999770508,
14: 0.031249999997705075,
15: 0.12499999999865749,
16: 0.03124999999966442,
17: 0.03124999999966442,
18: 0.031249999999664425,
19: 0.03124999999966442}
我通过以下方式解决了此问题
nx.set_node_attributes(G, 'Pi', 1)
for i,value in enumerate(PiList):
G.nodes[i]['Pi'] = PiDict[i]
我之前的代码有什么问题?有正确的解决方法吗?
答案 0 :(得分:1)
尝试不同的参数顺序。
nx.set_node_attributes(G, PiDict, 'Pi')
您的解决方案要做的是为每个节点赋予一个名称为1
且值为'Pi'
的属性。然后,为每个节点分配另一个属性'Pi'
,该属性的值就是所需的值。要进行检查,请在分配属性后执行G.nodes(data=True)
。