gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:56:31 25 4
gpt4 key购买 nike

本题设置为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/

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