Elegant access to edge attributes in networkx

前端 未结 2 1559
再見小時候
再見小時候 2021-02-09 00:09

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?



        
相关标签:
2条回答
  • 2021-02-09 00:45

    In NetworkX 2.4 you can also use graph.edges.data().

    So in this case:

    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():
        print(data['weight'])
    

    The output is

    4.7
    5.8
    
    0 讨论(0)
  • 2021-02-09 00:52

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

    prints

    4.7
    5.8
    
    0 讨论(0)
提交回复
热议问题