添加节点时出现 NetworkX 错误:"unhashable type: 'dict'"和"ValueError: too many values to unpack (expected 2)"



在运行下面的NetworkX python 3.5代码(在Jupyter中(时,我得到了一个我不太理解的错误。任何帮助将不胜感激。

import pandas as pd
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import os, warnings     
warnings.filterwarnings('ignore')
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

## NODES
a = [ (  'a_%d' % i, {'a' : i}) for i in range(0,5) ]
b = [ (  'b_%d' % i, {'b' : i}) for i in range(0,5) ]
c = [ (  'c_%d' % i, {'c' : i}) for i in range(0,5) ]
d = [ (  'd_%d' % i, {'d' : i}) for i in range(0,5) ]
E = [ (  'E_%d' % h, {'a': i}, {'b': j}, {'c': k}, {'d': l} )
        for h in range(1,626) for i in range(0,5) 
        for j in range(0,5) for k in range(0,5) 
        for l in range(0,5) ]
## GRAPH initialization
testgraph = nx.Graph()
list_of_nodegroups = [a, b, c , d, E]

这就是它失败的地方:

## GRAPH construction - adding nodes
for ng1 in list_of_nodegroups1:
    testgraph.add_nodes_from(ng1)

我收到此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/networkx/classes/graph.py in add_nodes_from(self, nodes_for_adding, **attr)
    535             try:
--> 536                 if n not in self._node:
    537                     self._adj[n] = self.adjlist_inner_dict_factory()
TypeError: unhashable type: 'dict'
During handling of the above exception, another exception occurred:
ValueError                                Traceback (most recent call last)
<ipython-input-10-718f064c86a3> in <module>()
      1 for ng1 in list_of_nodegroups:
----> 2     testgraph.add_nodes_from(ng1)
/usr/local/lib/python3.5/dist-packages/networkx/classes/graph.py in add_nodes_from(self, nodes_for_adding, **attr)
    540                     self._node[n].update(attr)
    541             except TypeError:
--> 542                 nn, ndict = n
    543                 if nn not in self._node:
    544                     self._adj[nn] = self.adjlist_inner_dict_factory()
    ValueError: too many values to unpack (expected 2)

参考(据我了解(两件事:

    ">
  • 不可散列类型:"字典"
  • "值错误:要解压缩的值太多(预期 2(">

我正在尝试弄清楚如何解决这个问题,无论是在此代码中还是在其他地方,非常感谢任何帮助!

编辑:

我的目标是有一个由 4 种类型的"子节点"组成的图,即 a、b、c、d 和 1 种类型的"母节点"(即 E(,如下所示:

[ ('E1', {'a0': 0, 'b0': 0, 'c0': 0, 'd0': 0} ),
  ('E2', {'a1': 1, 'b0': 0, 'c0': 0, 'd0': 0} ),
  ('E3', {'a1': 1, 'b1': 1, 'c0': 0, 'd0': 0} ),
   ...
  ('E625', {'a5': 5, 'b5': 5, 'c5': 5, 'd5': 5} ),
]

> Graph.add_nodes_from(nodes_for_adding, **attr)文档:nodes_for_adding(可迭代容器(——节点的容器(列表、字典、集合等(。OR (节点、属性字典(元组的容器。节点属性使用属性字典进行更新。

列表中的每个元素E都是一个包含 1 个节点和 4 个字典的元组,而add_nodes_from只接受一个字典。这 4 个词典应合并为一个词典:{'a': i, 'b': j, 'c': k, 'd': l} .

此外,列表E有 625 个× 5⁴ = 390625 个元素。这是您的意图,还是您正在尝试枚举节点?如果要枚举,它不会按预期工作。相反,请使用

from itertools import product
E = [('E_%d' % h, {'a': i, 'b': j, 'c': k, 'd': l})
     for h, (i, j, k, l) in enumerate(product(range(5), repeat=4), start=1)]

最新更新