我想要做的是比较节点和边缘中的键。我已经创建了一个MultiDiGraph,其中包含我称为节点的点的坐标,其中键是该点的name
,其代码中列出了下面的属性。然后,我创建了带有station_name
和target_name
的边作为参考,这些参数由我在其他地方创建的observations
类的数据填充,以存储数据。这样,每条边都可以是传出光线或传入光线。数据从文件读入并基于python中的内置字典类(2.7x)存储在一系列类中,这些类已被视为下面的对象,即knowncoords
和{{1}使用observations
作为简单的值持有者类。
我的代码的NetworkX部分设置如下:
station
以上输出如下:
def Execute(self):
G = nx.MultiDiGraph()
#Adding known points as nodes to G
for name, s in knowncoords.iteritems():
#Below "Known":True because it is in the file, "OrientCorrn"
#still has to be calculated for each station below, and I still
#have to cycle through the points to check them hence "Checked"=False
attr = {"Known":True,"Checked":False,"OrientCorr":0.0}
G.add_node(name,attr)
print G.nodes(data=True),"\n"
#print G.number_of_nodes()
#print G.nodes(data=False)#Prints keys only
#print G.nodes(data=True)#Prints keys with data as list
#print G.node#Prints all nodes with key and data as embedded dict
#Adds observations as edges based on station and targets
for station_name,station in observations.iteritems():
for target_name,target in station.iteritems():
G.add_edge(station_name,target_name)
G.edge[station_name][target_name] = {target_name:
(target.HA,
target.VA, target.HD)}
#print G.get_edge_data(station_name,target_name)[0]
print G.edges(data=True)
我现在要做的是将节点中的[('WTOP', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('KB', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('TS8', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('STEP', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('WBOT', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('CNSTA', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('TS7', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('DP', {'Known': True, 'OrientCorr': 0.0, 'Checked': False}), ('FRNWD', {'Known': True, 'OrientCorr': 0.0, 'Checked': False})]
[('WTOP', 'TS7', (0.8767806941497847, None, None)), ('WTOP', 'STEP', (0.3830609857182666, None, None)), ('WTOP', 'N5', (2.2121078641665908, None, 62.281)), ('WTOP', 'TS8', (4.882321023750393, None, None)), ('TS8', 'WTOP', (1.098965956065474, None, 41.425)), ('TS8', 'N1', (2.6658692290010606, None, 116.121)), ('TS8', 'DP', (1.9014004722171114, None, None)), ('TS8', 'WBOT', (5.6203528905034394, None, 36.558)), ('N1', 'N2', (0.859046209694798, None, 271.342)), ('N1', 'DP', (0.6897638166617812, None, None)), ('N1', 'TS8', (4.640059627299959, None, 116.021)), ('N1', 'FRNWD', (6.1694140806336115, None, None)), ('N2', 'N1', (5.266419510746235, None, 271.418)), ('N2', 'N3', (1.0780607901360308, None, 166.267)), ('N2', 'CNSTA', (0.5640807179709452, None, None)), ('N2', 'FRNWD', (1.0797770305671586, None, None)), ('N2', 'DP', (1.9260968811328312, None, None)), ('N3', 'N2', (5.326260063405584, None, 166.258)), ('N3', 'N4', (5.86409296868126, None, 193.935)), ('N3', 'FRNWD', (2.1863642576996742, None, None)), ('N3', 'DP', (3.1192912242587547, None, None)), ('N4', 'N3', (5.294305993683654, None, 193.9380377)), ('N4', 'FRNWD', (4.789624647922251, None, None)), ('N4', 'DP', (5.645577746331569, None, None)), ('N4', 'N5', (3.048295108797074, None, 213.277)), ('N5', 'WTOP', (4.892555440558616, None, 62.282)), ('N5', 'N4', (2.384876067566785, None, 213.275)), ('N5', 'FRNWD', (1.2586829751701993, None, None)), ('N5', 'DP', (2.1078729227280406, None, None))]
键与边缘中的name
键进行比较,如果它们相等则执行计算并更新target_name
值节点。即我在WTOP设置观察到TS7,我想检查TS7是否在我的节点中,如果是,那么我可以根据它执行一些计算来更新WTOP节点的“OrientCorr”值。
我已经尝试了OrientCorrn
但是这不会返回我预期的G.get_edge_data(station_name,target_name)[0]
键,而是返回上面target_name
的值,我可以使用target.HA
来获取节点的键,但我如何迭代它们?或者有没有办法检查所有节点的所有边缘,只返回符合我的条件的匹配项,如:
G.nodes(data=False)
提前致谢
答案 0 :(得分:2)
事实证明我正在严重创建边缘,即没有每个属性的键。我通过更改下面的代码来修复它,该代码根据station_name
和target_name
创建了一条边,但所有属性(target_name
)都有一个关键字:
G.add_edge(station_name,target_name)
G.edge[station_name][target_name] = {target_name:(target.HA,
target.VA, target.HD)}
对此:
G.add_edge(station_name,
target_name,
target=target_name,
HA=target.HA,
VA=target.VA,
HD=target.HD)
这让我可以像以前一样根据station_name
和target_name
调用每个边缘,但现在我可以根据相关边缘的特定键调用特定属性。然后,我使用以下循环来调用我想要的名称(observations
和knowncoords
字典中的名称,我基于边缘和节点):
for station_name, station in observations.iteritems():
for target_name, target in station.iteritems():
for name, s in knowncoords.iteritems():
if (G.get_edge_data(station_name,target_name)[0]['target']) == name:
print '@',station_name,'Target: ',
G.get_edge_data(station_name,target_name)[0]['target'],
'which matches with', name, 'in knowncoords'
这给了我想要的输出:
@ WTOP Target: TS7 which matches with TS7 in knowncoords
@ WTOP Target: STEP which matches with STEP in knowncoords
@ WTOP Target: TS8 which matches with TS8 in knowncoords
@ TS8 Target: WTOP which matches with WTOP in knowncoords
@ TS8 Target: WBOT which matches with WBOT in knowncoords
@ TS8 Target: DP which matches with DP in knowncoords
@ N1 Target: TS8 which matches with TS8 in knowncoords
@ N1 Target: DP which matches with DP in knowncoords
@ N1 Target: FRNWD which matches with FRNWD in knowncoords
@ N2 Target: DP which matches with DP in knowncoords
@ N2 Target: FRNWD which matches with FRNWD in knowncoords
@ N2 Target: CNSTA which matches with CNSTA in knowncoords
@ N3 Target: DP which matches with DP in knowncoords
@ N3 Target: FRNWD which matches with FRNWD in knowncoords
@ N4 Target: DP which matches with DP in knowncoords
@ N4 Target: FRNWD which matches with FRNWD in knowncoords
@ N5 Target: WTOP which matches with WTOP in knowncoords
@ N5 Target: DP which matches with DP in knowncoords
@ N5 Target: FRNWD which matches with FRNWD in knowncoords
然而,所有的信用都归于@EdChum,因为我指的是正确的方向!