我写了一个函数,从边的列表(取自数据库)创建图形。我使用图形库工具。Python和这个库对我来说都是全新的。在
图中的每个顶点都应该用一对字符串和一对数字来描述。在函数中,如果我考虑向图中添加新顶点,首先检查图中是否存在具有相同属性的顶点。为此,我使用find_vertex函数。我不明白为什么会出现打字错误,请帮忙。这里有这个函数的代码和回溯(如下):
编辑:还有一件事我必须处理unicode字符串。在
def make_graph():
conn = get_connection()
cursor = conn.cursor()
cursor.execute(sql)
wordnetList = cursor.fetchall()
g= Graph(directed=False)
vprop = g.new_vertex_property("python::object")
g.vertex_properties['lexicalunit'] = vprop
for (hyponym, v1, hyperonym, v2) in wordnetList: # hyponym and v1 (string and integer respectively) are properties of first vertex, hyperonym and v2 for the second
matched1 = find_vertex(g, g.vp['lexicalunit'], (hyponym, v1)) # this is line with problem
if len(matched1) == 0:
ver1 = g.add_vertex()
vprop[ver1] = (hyponym, v1)
elif len(matched1) >= 1:
ver1 = matched1[0]
matched2 = find_vertex(g, g.vp['lexicalunit'], (hyperonym, v2))
if len(matched2) == 0:
ver2 = g.add_vertex()
vprop[ver2] = (hyperonym, v2)
elif len(matched2) >= 1:
ver2 = matched2[0]
g.add_edge(ver1, ver2)
return g
回溯:
^{pr2}$
这是图形工具中的一个错误。现在在git版本中已经修复了:http://git.skewed.de/graph-tool/commit/?id=566d6dd816e167e1c9e824961537301ee1527e14
相关问题 更多 >
编程相关推荐