元组索引必须是整数,而不是 str
tuple indices must be integers, not str
我一直在网上收到这个错误:
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()
看起来你确实有一个元组,而不是你期望的字典。
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
此代码 returns 只有一个 <class 'dict'>
- 因此列表中可能有您不知道的内容
我一直在网上收到这个错误:
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()
看起来你确实有一个元组,而不是你期望的字典。
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
此代码 returns 只有一个 <class 'dict'>
- 因此列表中可能有您不知道的内容