代码之家  ›  专栏  ›  技术社区  ›  Alexander

提取节点属性最大值的键

  •  0
  • Alexander  · 技术社区  · 2 年前

    在本例中,在获得节点属性的最大值后,我们希望将其键放入或添加到单独的字典或列表中。 例如,如果在节点中有1个节点的属性值是最高值,则1本身将被添加到新的字典或列表中。如何做到这一点。谢谢

    import networkx as nx
    
    G = nx.read_gml('./networks/karate.gml',label=None)
    bb = nx.betweenness_centrality(G)
    cc = nx.closeness_centrality(G)
    nx.set_node_attributes(G,bb,"Betweenness")
    nx.set_node_attributes(G,cc,"Closeness")
    
    for g in G.nodes():
        continue
    print(max(G.nodes(data=True), key=lambda x: x[1]['Closeness']))
    

    需要注意的是,Continue命令在上述代码中没有特殊用途,您应该假设我们使用了print命令。事实上,我们指的是我们一开始所说的解释。

    这里,节点1具有最高的特征值。现在,如何提取1并将其添加到新的列表或词典中?

    (1, {'Betweenness': 0.43763528138528146, 'Closeness': 0.5689655172413793})
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   Nox5692    2 年前

    您可以简单地如下索引元组:

    max_node = max(G.nodes(data=True), key=lambda x: x[1]['Closeness'])
    key = max_node[0];