Python-';对象不能解释为索引';错误
我在Dijkstra算法代码中遇到了一个我不理解的错误-以下是错误消息:Python-';对象不能解释为索引';错误,python,dijkstra,Python,Dijkstra,我在Dijkstra算法代码中遇到了一个我不理解的错误-以下是错误消息: Traceback (most recent call last): File "C:\Documents and Settings\Harvey\Desktop\algorithm.py", line 52, in <module> tentativeDistance(currentNode,populateNodeTable()) File "C:\Documents and Setting
Traceback (most recent call last):
File "C:\Documents and Settings\Harvey\Desktop\algorithm.py", line 52, in <module>
tentativeDistance(currentNode,populateNodeTable())
File "C:\Documents and Settings\Harvey\Desktop\algorithm.py", line 29, in tentativeDistance
currentDistance = nodeTable[currentNode].distFromSource + network[currentNode][nearestNeighbour] #gets current distance from source
TypeError: object cannot be interpreted as an index
回溯(最近一次呼叫最后一次):
文件“C:\Documents and Settings\Harvey\Desktop\algorithm.py”,第52行,在
TentiveInstance(currentNode,populateNodeTable())
文件“C:\Documents and Settings\Harvey\Desktop\algorithm.py”,第29行,处于保留状态
currentDistance=nodeTable[currentNode]。distFromSource+网络[currentNode][NearestNeighbor]#获取与源的当前距离
TypeError:对象不能解释为索引
这是我的密码:
infinity = 1000000
invalid_node = -1
startNode = 0
class Node:
distFromSource = infinity
previous = invalid_node
visited = False
def populateNodeTable():
nodeTable = []
index =0
f = open('route.txt', 'r')
for line in f:
node = map(int, line.split(','))
nodeTable.append(Node())
print nodeTable[index].previous
print nodeTable[index].distFromSource
index +=1
nodeTable[startNode].distFromSource = 0
#currentNode = nodeTable[startNode]
return nodeTable
def tentativeDistance(currentNode, nodeTable):
nearestNeighbour = []
#j = nodeTable[startNode]
for currentNode in nodeTable:
currentDistance = nodeTable[currentNode].distFromSource + network[currentNode][nearestNeighbour] #gets current distance from source
if currentDistance != 0 & NodeTable[currentNode].distFromSource < Node[currentNode].distFromSource:
nodeTable[currentNode].previous = currentNode
nodeTable[currentNode].length = currentDistance
nodeTable[currentNode].visited = True
nodeTable[currentNode] +=1
nearestNeighbour.append(currentNode)
print nearestNeighbour
return nearestNeighbour
currentNode = startNode
if __name__ == "__main__":
populateNodeTable()
tentativeDistance(currentNode,populateNodeTable())
infinity=1000000
无效的_节点=-1
startNode=0
类节点:
距离源=无穷大
previous=无效的_节点
访问=错误
def populateNodeTable():
nodeTable=[]
索引=0
f=打开('route.txt','r')
对于f中的行:
node=map(int,line.split(','))
nodeTable.append(Node())
打印节点表[索引]。上一页
打印节点表[index].distFromSource
指数+=1
节点表[startNode].distFromSource=0
#currentNode=nodeTable[startNode]
返回节点表
定义保持状态(currentNode,nodeTable):
近邻=[]
#j=节点表[startNode]
对于节点表中的currentNode:
currentDistance=nodeTable[currentNode]。distFromSource+网络[currentNode][NearestNeighbor]#获取与源的当前距离
如果当前距离!=0&NodeTable[currentNode]。distFromSource
我的第一个函数执行正确,第二个函数的逻辑正确,尽管在线搜索解决方案被证明是徒劳的鉴于
for
循环在Python中的工作方式,您不必编写
for currentNode in nodeTable:
currentDistance = nodeTable[currentNode].distFromSource + network[currentNode][nearestNeighbour] #gets current distance from source
你应该写:
for currentNode in nodeTable:
currentDistance = currentNode.distFromSource + network[currentNode][nearestNeighbour]
假设network是一个带有键节点的字典,这将很好地工作。鉴于
for
循环在Python中的工作方式,您不必编写
for currentNode in nodeTable:
currentDistance = nodeTable[currentNode].distFromSource + network[currentNode][nearestNeighbour] #gets current distance from source
你应该写:
for currentNode in nodeTable:
currentDistance = currentNode.distFromSource + network[currentNode][nearestNeighbour]
假设网络是一个包含键节点的字典,这将很好地工作