Python - Networkx:具有一定权重的邻居
Python - Networkx: Neighbours with certain weight
本题设置为python 2.7 using the package networkx: https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.neighbors.html
我正在寻找一种使用函数 "networkx.Graph.neighbors" 的方法,该函数 returns 我所有具有特定权重值(或 higher/lower 比特定值)的邻居。
有什么建议可以实现吗?
提前致谢:)。
蒂姆
假设您要根据 weight.Creating 下图过滤 '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)])
给出:
{'weight':0.7}
{'weight':0.9}
希望这能回答您的问题。
如果您需要有关如何使用权重的更多信息,请参阅 link。
https://networkx.github.io/documentation/stable/auto_examples/drawing/plot_weighted_graph.html
我假设 "all the neighbors with a certain weight value" 指的是节点权重。我将执行查找权重大于特定值的邻居的示例。
import networkx as nx
import random
G = nx.Graph()
#now create nodes with random weights. If this notation is
#unfamiliar, read up on list comprehensions. They make life much easier.
nodes = [(node, {'weight':random.random()}) for node in range(10)]
G.add_nodes_from(nodes)
#now G has the nodes and they each have a random weight.
G.nodes(data=True)
> [(0, {'weight': 0.42719462610483916}),
(1, {'weight': 0.13985473528922154}),
(2, {'weight': 0.06889096983404697}),
(3, {'weight': 0.10772762947744585}),
(4, {'weight': 0.24497933676194383}),
(5, {'weight': 0.18527691296273396}),
(6, {'weight': 0.16379510964497113}),
(7, {'weight': 0.5481883941716088}),
(8, {'weight': 0.3782931298078134}),
(9, {'weight': 0.5902126428368549})]
#now create edges from 0 to all other nodes
zero_edges = [(0,u) for u in range(1,10)]
G.add_edges_from(zero_edges)
#now find all neighbors of 0 with weight > 0.5
heavy_neighbors = [nbr for nbr in G.neighbors(0) if G.node[nbr]['weight']>0.5]
heavy_neighbors
>[7,9]
如果您愿意,也可以通过将外部 [
和 ]
替换为 (
和 )
来使 heavy_neighbors
成为生成器。
本题设置为python 2.7 using the package networkx: https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.neighbors.html
我正在寻找一种使用函数 "networkx.Graph.neighbors" 的方法,该函数 returns 我所有具有特定权重值(或 higher/lower 比特定值)的邻居。
有什么建议可以实现吗?
提前致谢:)。 蒂姆
假设您要根据 weight.Creating 下图过滤 '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)])
给出: {'weight':0.7} {'weight':0.9} 希望这能回答您的问题。 如果您需要有关如何使用权重的更多信息,请参阅 link。 https://networkx.github.io/documentation/stable/auto_examples/drawing/plot_weighted_graph.html
我假设 "all the neighbors with a certain weight value" 指的是节点权重。我将执行查找权重大于特定值的邻居的示例。
import networkx as nx
import random
G = nx.Graph()
#now create nodes with random weights. If this notation is
#unfamiliar, read up on list comprehensions. They make life much easier.
nodes = [(node, {'weight':random.random()}) for node in range(10)]
G.add_nodes_from(nodes)
#now G has the nodes and they each have a random weight.
G.nodes(data=True)
> [(0, {'weight': 0.42719462610483916}),
(1, {'weight': 0.13985473528922154}),
(2, {'weight': 0.06889096983404697}),
(3, {'weight': 0.10772762947744585}),
(4, {'weight': 0.24497933676194383}),
(5, {'weight': 0.18527691296273396}),
(6, {'weight': 0.16379510964497113}),
(7, {'weight': 0.5481883941716088}),
(8, {'weight': 0.3782931298078134}),
(9, {'weight': 0.5902126428368549})]
#now create edges from 0 to all other nodes
zero_edges = [(0,u) for u in range(1,10)]
G.add_edges_from(zero_edges)
#now find all neighbors of 0 with weight > 0.5
heavy_neighbors = [nbr for nbr in G.neighbors(0) if G.node[nbr]['weight']>0.5]
heavy_neighbors
>[7,9]
如果您愿意,也可以通过将外部 [
和 ]
替换为 (
和 )
来使 heavy_neighbors
成为生成器。