简体   繁体   English

如何设置networkx的“read_edgelist”设置默认自定义权重?

[英]How to `read_edgelist` of networkx set default custom weight?

I'm using networkx to read edges from file.我正在使用 networkx 从文件中读取边缘。 My data likes:我的数据喜欢:

1 2
1 3
2 4

How to set custom weight when read from above data?从上述数据中读取时如何设置自定义重量? I want to my data likes:我想要我的数据喜欢:

1 2 1.0
1 3 1.0
2 4 1.0

or weight may be other value which can be chose without weight in original data.或者权重可以是原始数据中没有权重可以选择的其他值

You could build a weighted edge list while reading the file into a nested list, and then feed the weighted edges into a graph with add_weighted_edges_from :您可以在将文件读入嵌套列表时构建加权边列表,然后使用add_weighted_edges_from将加权边馈送到图形中:

weight = 1
edges = [[*map(int,line.split()), weight] for line in open("file.txt")]
G = nx.Graph()
G.add_weighted_edges_from(edges)
G.edges(data=True)
# EdgeDataView([(1, 2, {'weight': 1}), (1, 3, {'weight': 1}), (2, 4, {'weight': 1})])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM