python - 用给定的元组列表构造邻接矩阵
问题描述
我有以下元组列表
[(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)”处不可迭代
解决方案
推荐阅读
- django - 对代码进行更改不会反映在 django webapp 上
- vuejs3 - 为什么 toRaw(obj) 保持反应性?
- python - “on_raw_reaction_add”工作但“on_raw_reaction_remove”不返回公会?
- sql - SQL Max 函数返回两个结果,尽管只需要其中一个
- node.js - html-pdf 调整内容以适应 PDF
- linux - 计数器循环不会在期望内递增
- apache-kafka - 使用 SASL_SSL/OAUTHBEARER 的集群中的 Kafka Producer 错误
- php - Laravel 语言环境不采用其他日期格式
- python - 如何回复我输入的频道之外的消息?
- javascript - Vuejs - 通过 lodash 去抖动,传递“等待”参数