首页 > 解决方案 > 用给定的元组列表构造邻接矩阵

问题描述

我有以下元组列表

[(772, 36, 3),
 (471, 228, 5),
 (641, 401, 4),
 (312, 98, 4),
 (58, 504, 5),
 (235, 727, 5),
 (285, 266, 4),
 (451, 513, 4),
 (436, 661, 5),
 (101, 648, 3),
 (4, 229, 3),
 (159, 174, 4),
 (832, 1141, 4),
 (53, 544, 3),
 (755, 471, 3),
 (886, 1049, 4),
 (931, 574, 2),
 (641, 582, 4),
 (585, 181, 3),
 (853, 456, 3)]

我想使用 tuple[0] 和 tuple[1] 作为矩阵维度和 tuple[2] 作为其中的值来创建邻接矩阵。

我的代码是

import networkx as nx
from networkx.algorithms.bipartite import biadjacency_matrix

def to_adjacency_matrix(data,edges):
    g = nx.DiGraph()
    g.add_edges_from(edges)
    partition_1 = set(map(itemgetter(0), data))
    return biadjacency_matrix(g, partition_1).toarray()

to_adjacency_matrix(data,edges)

将异常作为 TypeError:“int”对象在“g.add_edges_from(edges)”处不可迭代

标签: pythonadjacency-matrixbipartite

解决方案


推荐阅读