Networkx:NetworkXException:节点列表包含 stochastic_block_model 的重复项

Networkx: NetworkXException: nodelist contains duplicate for stochastic_block_model

我是 networkx(2.4 版)的新手,对 stochastic_block_model 尝试添加节点列表时出现的错误感到有些困惑。我正在尝试使用以下代码为网络中的每个块设置不同的颜色属性:

import networkx as nx
N_p = 10
N_n = 10
N_0 = 30
sizes = [N_p, N_n, N_0]
probs = [[0.25, 0.05, 0.02],
         [0.05, 0.35, 0.07],
         [0.02, 0.07, 0.40]]
nodelist = ['blue' for i in range(N_p)]
nodelist.extend(['red' for i in range(N_n)])
nodelist.extend(['green' for i in range(N_0)])
G = nx.stochastic_block_model(sizes, probs,nodelist=nodelist, seed=0,directed=1)

但我收到以下错误消息:

...
/opt/anaconda3/lib/python3.7/site-packages/networkx/generators/community.py in stochastic_block_model(sizes, p, nodelist, seed, directed, selfloops, sparse)
    576             raise nx.NetworkXException("'nodelist' and 'sizes' do not match.")
    577         if len(nodelist) != len(set(nodelist)):
--> 578             raise nx.NetworkXException("nodelist contains duplicate.")
    579     else:
    580         nodelist = range(0, sum(sizes))

NetworkXException: nodelist contains duplicate.

我做错了什么?

错误只是 - 节点列表包含重复项:

>>> nodelist
['blue'*10, 'red'*10, 'green'*30]

如您的文档所述link:

Raises NetworkXError –

If probabilities are not in [0,1]. If the probability matrix is not square (directed case). If the probability matrix is not symmetric (undirected case). If the sizes list does not match nodelist or the probability matrix. If nodelist contains duplicate.

要解决此问题,请不要使用节点列表,或者执行以下操作:

nodelist = [f'blue_{i}' for i in range(N_p)]
nodelist.extend([f'red_{i}' for i in range(N_n)])
nodelist.extend([f'green_{i}' for i in range(N_0)])