将 edgelist 的值替换为 labels 字典的值
Replacing the values of `edgelist` with those of a `labels` dictionary
我是 Python 和 NetworkX 的新手。我有一个带有 NxN
个节点( 格子 )的方形正方形图 G
。这些节点用 dict
标记(见下面的代码)。现在我希望每个边的 edgelist
到 return start 和 endpoint 不是通过引用节点坐标而是给节点的标签。
示例:
N = 3
G=nx.grid_2d_graph(N,N)
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
#This gives nodes an attribute ID that is identical to their labels
for (i,j) in labels:
G.node[(i,j)] ['ID']= labels[(i,j)]
edgelist=G.edges() #This gives the list of all edges in the format (Start XY, End XY)
如果我 运行 它与 N=3
我得到:
In [14]: labels
Out[14]: {(0, 0): 6, (0, 1): 3, (0, 2): 0, (1, 0): 7, (1, 1): 4, (1, 2): 1, (2, 0): 8, (2, 1): 5, (2, 2): 2}
该方案将左上角的节点标记为0
,节点(N-1)th
放在右下角。这就是我想要的。 edgelist
的问题:
In [15]: edgelist
Out [15]: [((0, 1), (0, 0)), ((0, 1), (1, 1)), ((0, 1), (0, 2)), ((1, 2), (1, 1)), ((1, 2), (0, 2)), ((1, 2), (2, 2)), ((0, 0), (1, 0)), ((2, 1), (2, 0)), ((2, 1), (1, 1)), ((2, 1), (2, 2)), ((1, 1), (1, 0)), ((2, 0), (1, 0))]
我试图用这些行来解决问题(灵感来自这里:Replace items in a list using a dictionary):
allKeys = {}
for subdict in (labels):
allKeys.update(subdict)
new_edgelist = [allKeys[edge] for edge in edgelist]
但我得到了启发我星期一的这件美妙的事情:
TypeError: cannot convert dictionary update sequence element #0 to a sequence
总而言之,我希望能够用 labels
字典的值替换 edgelist
列表的元素,这样,例如,来自 ((2,0),(1,0))
的边(对应于节点 8 和 7)是 returned (8,7)
. 感激不尽!
我相信您正在寻找的只是 nx.relabel_nodes(G,labels,False)
这里是 documentation
Here is the output when I printed the nodes of G before and after calling the relabel nodes function.
# Before relabel_nodes
[(0, 1), (1, 0), (0, 0), (1, 1)]
# After relabel_nodes
[0, 1, 2, 3]
After doing this, the edge labels automatically becomes what you expect.
# Edges before relabelling nodes
[((0, 1), (0, 0)), ((0, 1), (1, 1)), ((1, 0), (0, 0)), ((1, 0), (1, 1))]
# Edges after relabelling nodes
[(0, 1), (0, 2), (1, 3), (2, 3)]
此外,我已经在您创建的聊天中回复了这个问题,但您似乎没有收到通知。
我是 Python 和 NetworkX 的新手。我有一个带有 NxN
个节点( 格子 )的方形正方形图 G
。这些节点用 dict
标记(见下面的代码)。现在我希望每个边的 edgelist
到 return start 和 endpoint 不是通过引用节点坐标而是给节点的标签。
示例:
N = 3
G=nx.grid_2d_graph(N,N)
labels = dict( ((i, j), i + (N-1-j) * N ) for i, j in G.nodes() )
#This gives nodes an attribute ID that is identical to their labels
for (i,j) in labels:
G.node[(i,j)] ['ID']= labels[(i,j)]
edgelist=G.edges() #This gives the list of all edges in the format (Start XY, End XY)
如果我 运行 它与 N=3
我得到:
In [14]: labels
Out[14]: {(0, 0): 6, (0, 1): 3, (0, 2): 0, (1, 0): 7, (1, 1): 4, (1, 2): 1, (2, 0): 8, (2, 1): 5, (2, 2): 2}
该方案将左上角的节点标记为0
,节点(N-1)th
放在右下角。这就是我想要的。 edgelist
的问题:
In [15]: edgelist
Out [15]: [((0, 1), (0, 0)), ((0, 1), (1, 1)), ((0, 1), (0, 2)), ((1, 2), (1, 1)), ((1, 2), (0, 2)), ((1, 2), (2, 2)), ((0, 0), (1, 0)), ((2, 1), (2, 0)), ((2, 1), (1, 1)), ((2, 1), (2, 2)), ((1, 1), (1, 0)), ((2, 0), (1, 0))]
我试图用这些行来解决问题(灵感来自这里:Replace items in a list using a dictionary):
allKeys = {}
for subdict in (labels):
allKeys.update(subdict)
new_edgelist = [allKeys[edge] for edge in edgelist]
但我得到了启发我星期一的这件美妙的事情:
TypeError: cannot convert dictionary update sequence element #0 to a sequence
总而言之,我希望能够用 labels
字典的值替换 edgelist
列表的元素,这样,例如,来自 ((2,0),(1,0))
的边(对应于节点 8 和 7)是 returned (8,7)
. 感激不尽!
我相信您正在寻找的只是 nx.relabel_nodes(G,labels,False)
这里是 documentation
Here is the output when I printed the nodes of G before and after calling the relabel nodes function.
# Before relabel_nodes
[(0, 1), (1, 0), (0, 0), (1, 1)]
# After relabel_nodes
[0, 1, 2, 3]
After doing this, the edge labels automatically becomes what you expect.
# Edges before relabelling nodes
[((0, 1), (0, 0)), ((0, 1), (1, 1)), ((1, 0), (0, 0)), ((1, 0), (1, 1))]
# Edges after relabelling nodes
[(0, 1), (0, 2), (1, 3), (2, 3)]
此外,我已经在您创建的聊天中回复了这个问题,但您似乎没有收到通知。