【问题标题】:Creating MultiDiGraph using networkx with .dat file give使用带有 .dat 文件的 networkx 创建 MultiDiGraph
【发布时间】:2018-11-05 06:22:09
【问题描述】:

您好,当我在 .dat 文件中给出图形表示时,我想知道如何使用 networkx 创建 MultiDigraph?该文件中的示例数据如下所示:

1 2 0.5
2 3 0.4
2 3 0.3
1 3 1.0

是否有任何内置函数可以做到这一点?或者我应该在哪里搜索有关它的任何有用信息?

【问题讨论】:

  • 通常库文档会提供有关支持的输入格式的所有相关信息。
  • 是的,这就是我所知道的,但实际上我找不到它:/

标签: python networkx graph-theory


【解决方案1】:

你可以使用read_edgelist:

import networkx as nx

graph = nx.MultiGraph()
nx.read_edgelist('edges.dat', create_using=graph, nodetype=int, data=(('weight', float),))

for u, v, _ in graph.edges:
    print(u, v, graph.get_edge_data(u, v))

输出

1 2 {0: {'weight': 0.5}}
1 3 {0: {'weight': 1.0}}
2 3 {0: {'weight': 0.4}, 1: {'weight': 0.3}}
2 3 {0: {'weight': 0.4}, 1: {'weight': 0.3}}

请注意,这会从名为 'edges.dat' 的文件中读取具有指定格式的图形:

1 2 0.5
2 3 0.4
2 3 0.3
1 3 1.0

该函数创建图表并将weight 作为属性放入字典字典中。

【讨论】:

  • 感谢您的复杂回答:D Greetings
猜你喜欢
  • 2015-08-26
  • 2022-10-13
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 2020-05-20
  • 2021-01-23
相关资源
最近更新 更多