python-2.7 - Python-网络x : Neighbours with certain weight

标签 python-2.7 networkx neighbours

本题设置为python 2.7,使用包networkx: https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.neighbors.html

我正在寻找一种使用函数“networkx.Graph.neighbors”的方法,它会返回所有具有特定权重值(或高于/低于特定值)的邻居。

有什么建议可以让这成为可能吗?

提前致谢:)。 蒂姆

最佳答案

假设您要根据权重过滤“c”个节点邻居。创建以下图表:

    G = nx.Graph()
    G.add_edge('a', 'b', weight=0.6)
    G.add_edge('a', 'c', weight=0.2)
    G.add_edge('c', 'd', weight=0.1)
    G.add_edge('c', 'e', weight=0.7)
    G.add_edge('c', 'f', weight=0.9)
    G.add_edge('a', 'd', weight=0.3)

    list_neighbors=G.neighbors('c')
    for i in list_neighbors:
        if G.edges[('c',i)]['weight']>0.5:
            print (G.edges[('c',i)])

给出: {'重量':0.7} {'重量':0.9} 希望这能回答您的问题。 如果您需要有关如何使用权重的更多信息,请参阅链接。 https://networkx.github.io/documentation/stable/auto_examples/drawing/plot_weighted_graph.html

关于python-2.7 - Python-网络x : Neighbours with certain weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50158530/

相关文章:

python - 计算数组中的邻居值

python - pdb:如何显示当前行,而不是在上一个列表之后继续?

python - 如何将连接最多的 networkx 节点放置在更靠近顶部的位置?

python - 相邻顶点之间的最短路径长度(不是任意两个随机顶点!)

matlab - 计算区域内部或外部的邻域

r - 为什么我从图中得到了错误的邻居

python - 使用python层Caffe实现Bhattacharyya损失函数

python - pandas 通过将列条目与多个其他列中的条目匹配来选择行

python - 在键为数字的字典中,如何找到较小但最接近给定数字的键?

python - networkx是否支持按标签进行dfs遍历