Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对象不可调用?_Python_Graph_Typeerror_Callable - Fatal编程技术网

Python 对象不可调用?

Python 对象不可调用?,python,graph,typeerror,callable,Python,Graph,Typeerror,Callable,大家好,我不知道如何解决这个问题,据我所知,这是因为我使用的对象的属性 请参考,我正在用Python绘制一个图,该图需要检查所有顶点和边是否连接 class graph(object): def __init__(self, gdict=None): if gdict == None: gdict = {} self.gdict = gdict # return the keys of the dictionary l

大家好,我不知道如何解决这个问题,据我所知,这是因为我使用的对象的属性

请参考,我正在用Python绘制一个图,该图需要检查所有顶点和边是否连接

class graph(object):

    def __init__(self, gdict=None):
        if gdict == None:
            gdict = {}
        self.gdict = gdict


    # return the keys of the dictionary list, or vertices
    def getVertice(self):
        return list(self.gdict.keys())

    # this allows us to obtain the edges for the graph
    # or "values" of the dict key
    def getEdges(self):
        return self.generate_edges()

    def addVertex(self, vertex):
        if vertex not in self.gdict:
            self.gdict[vertex] = []

    def addEdge(self, edge):
        edge = set(edge)
        (vertex1, vertex2) = tuple(edge)
        if vertex1 in self.gdict:
            self.gdict[vertex1].append(vertex2)
        else:
            self.gdict[vertex1] = [vertex2]

    # this generates a list of all edges for the vertices
    def generate_edges(self):
        edges = []
        for vertex in self.gdict:
            for neighbour in self.gdict[vertex]:
                if (neighbour, vertex) not in edges:
                    edges.append((vertex, neighbour))
        return edges

    def find_path(self, startVertex, endVertex, paths=None):
        if paths == None:
            paths = []
        graphs = self.gdict
        paths = paths + [startVertex]
        if startVertex == endVertex:
            return paths
        if startVertex not in graphs:
            return None
        for vertex in graphs[startVertex]:
            if vertex not in paths:
                extendedPath = self.find_path(vertex, endVertex, paths)

                if extendedPath:
                    return extendedPath
        return None

    def findAllPath(self, startVertex, endVertex, paths=None):
        if paths is None:
            paths = []
        graphs = self.gdict
        paths = paths + [startVertex]
        if startVertex == endVertex:
            return [paths]
        if startVertex not in graphs:
            return []
        paths = []
        for vertex in graphs[startVertex]:
            if vertex not in paths:
                extendedPath = self.find_path(vertex, endVertex, paths)
                for p in extendedPath:
                    paths.append(p)
            return paths

    def findisovertices(self):  ##reword this
        """ returns a list of isolated vertices. """
        graphs = self.gdict
        iso = []
        for vertex in graphs:
            print(iso, vertex)
            if not graphs[vertex]:
                iso += [vertex]
        return iso

    def isConnected(self, verticesMet=None, startVertex=None):
        if verticesMet is None:
            verticesMet = set()
        gdict = self.gdict
        vertices = self.gdict()
        if not startVertex:
            startVertex = vertices[0]
        verticesMet.add(startVertex)
        if len(verticesMet) != len(vertices):
            for vertex in gdict[startVertex]:
                if vertex not in verticesMet:
                    if self.isConnected(verticesMet, vertex):
                        return True
        else:
            return True
        return False
    # this function prints the nodes/vertices in the graph
    def completeGraph(self):
        Vertex = len(self.gdict.keys())
        Edges = len(self.gdict.values())
        answer = 2.0 * Edges / (Vertex * (Vertex - 1))
        return answer

graph_elements = ({"a": ["d", "f"],
                   "b": ["c"],
                   "c": ["b", "c", "d", "e"],
                   "d": ["a", "c"],
                   "e": ["c"],
                   "f": ["a"],
                   "z": []
                   })

g = graph(graph_elements)

print("Our vertices are: \n", g.getVertice())
print("#1 | Generate list of all edges: \n", graph.generate_edges(g))

##2 Function to calculate isolated nodes of graph.
isolated = graph.findisovertices(g)
print("#2 | Find isolated nodes:\n", isolated)

# 3.    Function to find a path from a start vertex to an end vertex
path = graph.find_path(g, "a", "c")
print("#3 | Find a path function: \n", path)

# 4.    Function to find all the paths between a start vertex to an end vertex
allPaths = graph.findAllPath(g, "a", "e")
print("#4 | All paths function:\n", allPaths)

# 5. Function to check if graph is connected
connect = graph(g)


print("#5 | Connected graph function \n", connect.isConnected(g))
我一直收到以下错误:

Traceback (most recent call last):
  File "graphsAssign6.py", line 160, in <module>
    print("#5 | Connected graph function \n", connect.isConnected(g))
  File "graphsAssign6.py", line 95, in isConnected
    vertices = self.gdict()
TypeError: 'graph' object is not callable
回溯(最近一次呼叫最后一次):
文件“graphassign6.py”,第160行,在
打印(“#5 |连接的图形函数\n”,connect.isConnected(g))
文件“GraphAssign6.py”,第95行,已断开连接
顶点=self.gdict()
TypeError:“图形”对象不可调用

不要创建新连接=图形(g)。您的断开连接应在g内工作。此外,不应该使用self.gdict()获取顶点。这毫无意义,而且您已经有了一个名为getVertice的函数用于该作业。

我能够运行您的代码。请确保没有与
图形
同名的任何其他变量导致对象被阴影。是否使用自己的点集运行该对象?我不敢相信这就是问题所在,伊玛·克里伊运行的代码与你上面的代码完全相同!您的文件中有没有其他未粘贴到上面的代码?它应该是
vertices=self.gdict
为什么不可以?OP通过执行connect=graph(g)生成一个图形的图形。这对我来说没有多大意义。错误是
self.gdict()
,我看不出制作新的
connect=graph(g)
与此有什么关系。非常感谢!它实际上运行了,我想我的打印声明是错误的。我以前尝试过getVertice函数,但由于打印错误,它无论如何都不起作用。
def isConnected(self, verticesMet=None, startVertex=None):
    if verticesMet is None:
        verticesMet = set()
    gdict = self.gdict
    vertices = self.getVertice()
    if not startVertex:
        startVertex = vertices[0]
    verticesMet.add(startVertex)
    if len(verticesMet) != len(vertices):
        for vertex in gdict[startVertex]:
            if vertex not in verticesMet:
                if self.isConnected(verticesMet, vertex):
                    return True
    else:
        return True
    return False



# 5. Function to check if graph is connected
print("#5 | Connected graph function \n", g.isConnected())