图中的Python类型错误_tool.find_顶点

2024-09-29 05:18:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个函数,从边的列表(取自数据库)创建图形。我使用图形库工具。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}$

Tags: 函数lenfindcursorv2vertexv1顶点