Python - “对象不能解释为索引”错误

发布于 2024-10-21 00:24:38 字数 1987 浏览 3 评论 0原文

我的 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 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

这是我的代码:

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())

我的第一个函数执行正确,并且我的第二个函数的逻辑是正确的,尽管在线搜索解决方案有事实证明无果而终

I am having an error that I don't understand in my Dijkstra Algorithm code - here is the error message:

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

Here is my code:

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())

My first function performs correctly, and my logic is correct for my second function though searching online for the solution has proved fruitless

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2024-10-28 00:24:38

鉴于 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]

假设网络是一个带有键节点的字典,那就可以正常工作。

Given the way for loops work in Python, you don't have to write

for currentNode in nodeTable:
    currentDistance = nodeTable[currentNode].distFromSource + network[currentNode][nearestNeighbour] #gets current distance from source

You should instead write:

for currentNode in nodeTable:
    currentDistance = currentNode.distFromSource + network[currentNode][nearestNeighbour]

Assuming network is a dictionary with nodes for keys, that will work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文