优雅地访问networkx中的边缘属性 [英] Elegant access to edge attributes in networkx

查看:822
本文介绍了优雅地访问networkx中的边缘属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否确实有必要在networkx中访问下面的笨拙的第三种形式的边缘属性,并且不需要更精巧的前两种形式的变体呢?

Is it indeed the case that to access edge attributes in networkx the awkward third form below is necessary and no variation of the more svelte first two forms will do?

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

这篇关于优雅地访问networkx中的边缘属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆