我使用 pygraphviz 生成以下图表:
graph "Multiport-Switches" {
node [color="#dddddd",
label="\N",
shape=record,
style=filled
];
Switch0 [label="<1> 1|<2> 2|<3> 3|<4> 4|<5> 5|<6> 6|<7> 7|<8> 8"];
Switch1 [label="<1> 1|<2> 2|<3> 3|<4> 4|<5> 5|<6> 6|<7> 7|<8> 8"];
Switch0:1 -- Switch1:1;
Switch0:2 -- Switch1:2;
Switch0:3 -- Switch1:3;
Switch0:4 -- Switch1:4;
Switch0:5 -- Switch1:5;
Switch0:6 -- Switch1:6;
Switch0:7 -- Switch1:7;
Switch0:8 -- Switch1:8;
}
如果我遍历边缘,我可以使用以下代码查看所有边缘:
for edge in G.edges():
print edge, edge.attr
正如您在下面的输出中看到的那样,边缘元组是“相同的”,但它们仍然由属性区分。如果我想选择一个特定的,我可以在迭代时比较每个边的不同的特定属性。
(u'Switch0', u'Switch1') {u'tailport': u'1', u'headport': u'1'}
(u'Switch0', u'Switch1') {u'tailport': u'2', u'headport': u'2'}
(u'Switch0', u'Switch1') {u'tailport': u'3', u'headport': u'3'}
(u'Switch0', u'Switch1') {u'tailport': u'4', u'headport': u'4'}
(u'Switch0', u'Switch1') {u'tailport': u'5', u'headport': u'5'}
(u'Switch0', u'Switch1') {u'tailport': u'6', u'headport': u'6'}
(u'Switch0', u'Switch1') {u'tailport': u'7', u'headport': u'7'}
(u'Switch0', u'Switch1') {u'tailport': u'8', u'headport': u'8'}
现在,如果我使用以下代码来获得优势:
edge = G.get_edge('Switch0', 'Switch1')
print edge, edge.attr
我只得到最后一条('Switch0', 'Switch1')
边,如下所示:
(u'Switch0', u'Switch1') {u'tailport': u'8', u'headport': u'8'}
(u'Switch0', u'Switch1')
除了最后一条边之外,有什么方法可以在不遍历所有边的情况下获得特定的边?通过在类似get_edge
或类似的方法中传递额外的属性参数可能吗?