python - 如何连接从 csv 文件读取的节点?

标签 python

我有节点列表信息及其相关性。我想通过一一添加节点来构建网络。这是我的节点信息:

 (149, 88)
 (139, 168)
 (215, 218)
 (218, 215)
 (429, 400)
 (400, 429)
 (207, 176)
 (176, 207)
 (31, 45)
 (45, 31)
 (411, 381)
 (381, 411)
 (393, 335)
 (335, 393)
 (287, 317)
 (317, 287)
 (41, 77)
 (98, 130)
 (370, 340)
 ...

我正在努力

  G.add_edge(149, 88)
  G.add_edge(139, 168)
  G.add_edge(215, 218)
  G.add_edge(218, 215)
  G.add_edge(429, 400)
  G.add_edge(400, 429)
  G.add_edge(207, 176) 
  G.add_edge(176, 207)
  G.add_edge(31, 45)
  G.add_edge(45, 31)
  G.add_edge(411, 381)
  G.add_edge(381, 411)
  G.add_edge(393, 335)
  G.add_edge(335, 393)
  G.add_edge(287, 317)
  G.add_edge(317, 287)
  G.add_edge(41, 77)
  ...

但是我有30000多个节点信息,不可能手动一一添加。

知道如何通过评论做到这一点吗? 谢谢。

最佳答案

您可以使用ast.literal_eval:

import ast
import networkx as nx
G = nx.Graph()
for i in open('filename.csv'):
   G.add_edge(*ast.literal_eval(i.strip('\n')))

关于python - 如何连接从 csv 文件读取的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57444491/

相关文章:

python - 在Python中使用正则表达式

python - 如何去掉小数点

python - 散点图按颜色分隔簇 matplotlib python

python - 复制内部格式 float64 uint64

python - 如何注释 Optional[int] 的包装器?难度 : when it's a parameter

python - 如果两点之间的距离低于某个阈值,则从列表中删除点

python - Django 网站,基本的 2d python 游戏

python - 使用列表和数组的 numpy 数组索引

python - 列表作为字典的键

python - MySQL-Python 安装 - 无法构建鸡蛋