Python认为对象实例是列表对象
所以我在图上实现BFS来检测所有的循环。我通过邻接列表实现了这个图。但是当我运行我的代码时,我得到了以下错误Python认为对象实例是列表对象,python,graph,breadth-first-search,Python,Graph,Breadth First Search,所以我在图上实现BFS来检测所有的循环。我通过邻接列表实现了这个图。但是当我运行我的代码时,我得到了以下错误 Traceback (most recent call last): File "C:\Python27\Data Structures\Graph\bfstree.py", line 228, in <module> main() File "C:\Python27\Data Structures\Graph\bfstree.py", l
Traceback (most recent call last):
File "C:\Python27\Data Structures\Graph\bfstree.py", line 228, in <module>
main()
File "C:\Python27\Data Structures\Graph\bfstree.py", line 223, in main
traverse(g.getVertex(2))
File "C:\Python27\Data Structures\Graph\bfstree.py", line 168, in traverse
while (x.getPred()):
AttributeError: 'list' object has no attribute 'getPred'
下面是遍历函数:
def traverse(y):
x = y
while (x.getPred()):
print(x.getId())
x = x.getPred()
print(x.getId())
以下是图形的邻接列表实现:
class Graph:
def __init__(self):
self.vertList = {} #this is the masterlist
self.numVertices = 0
def addVertex(self,key): #turn something into a Vertex object
self.numVertices = self.numVertices + 1
newVertex = Vertex(key)
self.vertList[key] = newVertex #maps vertex names to vertex objects
return newVertex
def getVertex(self,n):
if n in self.vertList:
return self.vertList[n] #returns the Vertex object
else:
return None
def __contains__(self,n):#tweak the built-in operator 'in'(containment check)
return n in self.vertList
def addEdge(self,f,t,cost = 0):
if f not in self.vertList: #if f is not a node in the graph
nv = self.addVertex(f)
if t not in self.vertList: #if t is not a node in the graph
nv = self.addVertex(t)
self.vertList[f].addNeighbor(self.vertList[t], cost)
def getVertices(self):
return self.vertList.keys()
def __iter__(self): # iterate over Vertex objects over the Graph
return iter(self.vertList.values())
class Vertex:
def __init__(self,key):
self.id = key
self.connectedTo={} #dictionary which contains all the other vertices it is connected to
self.pred = [] #for BFS tree / a list because we are dealing with cycles
self.color = "white" #for BFS tree
def addNeighbor(self,nbr,weight=0):
self.connectedTo[nbr] = weight #nbr is another Vertex object
def __str__(self):
#TODO: lookup how that for loop works
return str(self.id) + "connected to " + str([x.id for x in self.connectedTo])
def getConnections(self):
return self.connectedTo.keys()
def getId(self):
return self.id
def getWeight(self,nbr):
return self.connectedTo[nbr]
def getColor(self):
return self.color
def setColor(self,color):
self.color = color
def setPred(self,node):
self.pred.append(node)
def getPred(self):
if len(self.pred)>1:
return self.pred
elif len(self.pred) == 0:
return self.pred[0]
else:
return self.pred
为什么说g.getVertex(2)是一个列表对象?我很确定这是一个顶点对象。我甚至在main函数中打印出了类型,它说它是一个实例而不是列表对象。x=x。getPred()
是问题所在。while
循环中的第一次检查正常,但在第一次更新x
后,它会中断,然后重新检查
在实现时,
getPred
返回self.pred
(它从self.pred
返回一个值而不是整个值的唯一情况是断开;长度为0,您进行索引,因此它将引发indexer
)self.pred
是一个列表
用x.getPred()的结果替换x
,如下所示:
while (x.getPred()):
print(x.getId())
x = x.getPred()
x.getPred()
返回self.pred
:
def getPred(self):
if len(self.pred)>1:
return self.pred
elif len(self.pred) == 0:
return self.pred[0]
else:
return self.pred
(请注意,对于len(self.pred)==0
您尝试返回self.pred[0]
,这将引发索引器
异常)
self.pred
是一个列表:
class Vertex:
def __init__(self,key):
# ...
self.pred = [] #for BFS tree / a list because we are dealing with cycles
因此,您将x
替换为list
对象,然后在该list对象上循环调用x.getPred()
。将x更改为另一个对象/类型,位置为“x=x.getPred()”。如果len(self.pred)==0
为真,则self.pred[0]
将引发一个索引器。除此之外,Python不仅仅认为x
是一个列表,它知道它是一个列表。在代码中的某个地方,x.getPred()
返回一个列表。您从x.getPred()
返回self.pred
(一个列表),因此设置x=x.getPred()
显然会将x
设置为一个列表,之后x.getPred()
将失败。哦,我明白了。谢谢!!!
class Vertex:
def __init__(self,key):
# ...
self.pred = [] #for BFS tree / a list because we are dealing with cycles