如何解决对象不可调用?
大家好,我不知道该如何解决这个问题,而据我所知,这是因为我正在使用的对象的属性。
供您参考,我正在用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,paths)
if extendedPath:
return extendedPath
return None
def findAllPath(self,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,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,"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
解决方法
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())
不要建立新的connect = graph(g)。您的isConnected应该在g范围内工作。另外,您不应该使用self.gdict()获得顶点。这没有任何意义,并且您已经有一个名为getVertice的函数用于该作业。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。