python元祖迭代_如何在Python中迭代元组的堆栈
我嘗試在Python中使用預定義為的DepthFirstSearch類實現DepthFirstSearch算法:class Stack:
def __init__(self):
self.list = []
def push(self,item):
self.list.append(item)
def pop(self):
return self.list.pop()
def isEmpty(self):
return len(self.list) == 0
我還有一個功能:
^{pr2}$
它返回我們是否處于預定義的目標狀態,
功能:def getStartState(self):
return self.startState
返回元組(int,int),代理的位置,
和功能:def getSuccessors(self, state):
self._expanded += 1
return successors
它以((int,int),string,int)的形式返回代理的所有可用的下一個“移動”的元組,其中(int,int)是繼任者的狀態,string是方向(NSEW),int是繼任者的成本。在
到目前為止,我對實現GraphSearch的GraphSearch的實現是這樣的:def depthFirstSearch(problem):
closed = []
fringe = util.Stack()
fringe.push(problem)
i = 0
while not fringe.isEmpty():
currentState = fringe.pop()
print "current's successors:", currentState.getSuccessors(currentState.getStartState())
if currentState.isGoalState(currentState.getStartState()):
return currentState
if not (currentState in closed):
closed.append(currentState)
print "closed now includes:", closed[i].getStartState()
children = currentState.getSuccessors(currentState.getStartState())
print "children:", children
while not children.isEmpty():
fringe.push(children.pop())
print "fringe:" fringe
i += 1
當我意識到這個問題并沒有完成時。我完成了第一次迭代,然后第二次就停止了。以下是終端輸出:current's successors: [((5, 4), 'South', 1), ((4, 5), 'West', 1)]
closed now includes: (5, 5)
children: [((5, 4), 'South', 1), ((4, 5), 'West', 1)]
Traceback (most recent call last):
...
...
...
AttributeError: 'list' object has no attribute isEmpty
所以很明顯,我沒有正確地遍歷這個元組的列表,把它們作為兩個附加的元組轉移到邊緣。在
有什么想法我應該如何穿越這個具體的實現?在
總結
以上是生活随笔為你收集整理的python元祖迭代_如何在Python中迭代元组的堆栈的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哚怎么读 哚到底念啥
- 下一篇: python中的numpy函数算相关系数