gpt4 book ai didi

python - 优雅地访问 networkx 中的边缘属性

转载 作者:太空狗 更新时间:2023-10-29 23:58:09 25 4
gpt4 key购买 nike

要访问 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']

最佳答案

使用data=True :

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'])

打印

4.7
5.8

关于python - 优雅地访问 networkx 中的边缘属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35190296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com