【发布时间】:2014-05-25 19:34:12
【问题描述】:
我想做的是比较节点和边中的键。我创建了一个 MultiDiGraph,其中包含我知道的点的坐标作为节点,其中的关键是该点的 name,其属性在我的代码中列出如下。然后,我创建了带有station_name 和target_name 的边作为参考,这些参考由我在其他地方创建的用于存储数据的observations 类中的数据填充。这样,每条边都可以是出射光线或入射光线。数据从文件中读入并存储在基于python中内置字典类的一系列类中,(2.7x),这些类在下面被视为对象,即knowncoords和observations和@987654328 @ 作为一个简单的值持有者类。
我的代码的 NetworkX 部分设置如下:
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 键进行比较,如果它们相等,则执行计算并更新节点中的OrientCorrn 值。即我设置在 WTOP 观察 TS7,我想检查 TS7 是否在我的节点中,如果是,那么我可以基于此执行一些计算以更新 WTOP 节点的“OrientCorr”值。
我已经尝试过G.get_edge_data(station_name,target_name)[0],但这并没有返回我期望的target_name 键,而是上面target.HA 的值,我可以使用G.nodes(data=False) 来获取节点的键,但是我该怎么做遍历它们?或者有没有办法检查所有节点的所有边缘,并根据我的标准返回匹配的边缘,例如:
if (G.edge[station_name][target_name])==(G.node[name]):
#do stuff
提前致谢
【问题讨论】:
-
G.get_edge_data(station_name,target_name)[0][target_name]有效吗?你也可以添加G.edges(data=True)的结果吗 -
不,给了我一个 KeyError:
print G.get_edge_data(station_name,target_name)[0][target_name] KeyError: 0 -
你能编辑你的问题并添加
G.edges(data=True)和G.nodes(data=True)的输出 -
会做,但它确实不是最整洁的......将尝试从完整数据集中对其进行修剪
-
我在这里回答了一个类似的问题:stackoverflow.com/questions/22655805/… 所以如果我能看到属性的结构,那么我可以建议如何检索数据
标签: python-2.7 networkx