确实是这样的情况:要访问 networkx 中的边缘属性,下面尴尬的第三种形式是必要的,并且更简洁的前两种形式的任何变体都行吗?
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2, weight=4.7 )
G.add_edge(3, 4, weight=5.8 )
# for edge in G.edges():
# print edge['weight']
#
# for edge in G.edges():
# print G[edge]['weight']
for edge in G.edges():
print G.edge[edge[0]][edge[1]]['weight']
Use data=True https://networkx.github.io/documentation/latest/reference/generated/networkx.Graph.edges.html:
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2, weight=4.7)
G.add_edge(3, 4, weight=5.8)
for node1, node2, data in G.edges(data=True):
print(data['weight'])
prints
4.7
5.8
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)