我一直在线上发现错误:
state = node["state"]
然而这有效:
print node["state"]
获得我得到的类型时:
print(type(node))
--><type 'dict'>
--><type 'tuple'>
以下是完整代码:
explored = dict()
state = problem.getStartState()
frontier = util.Stack()
node = {}
node["parent"] = None
node["action"] = None
node["state"] = state
frontier.push(node)
while not frontier.isEmpty():
node = frontier.pop()
state = node["state"]
if problem.isGoalState(state) == True:
break
if state in explored:
continue
explored[state] = True
class Stack:
def __init__(self):
self.list = []
def push(self,item):
self.list.append(item)
def pop(self):
return self.list.pop()
答案 0 :(得分:0)
看起来你真的有一个元组,而不是你期望的字典。
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
state = 'blah'
explored = dict()
frontier = Stack()
node = {}
node["parent"] = None
node["action"] = None
node["state"] = state
frontier.push(node)
while not frontier.isEmpty():
node = frontier.pop()
print(type(node)) # <--- NEW
state = node["state"]
#if problem.isGoalState(state) == True:
#break
#if state in explored:
#continue
explored[state] = True
此代码仅返回<class 'dict'>
- 因此列表中可能存在您不知道的内容