Int 对象不可迭代

发布于 2024-10-18 14:44:19 字数 1233 浏览 3 评论 0原文

我遇到了一个涉及 Dijkstra 算法的问题,我不知道如何解决 - 这是我的代码:

infinity = 1000000
invalid_node = -1
#startNode = 0

class Node:
    distFromSource = infinity
    previous = invalid_node
    visited = False

def populateNodeTable():
     nodeTable = []
     f = open("twoDArray.txt", "r")
     for line in f.readlines(): #get number of nodes from file
       nodeTable.append(line.split(','))   # Create matrix of weights

     numNodes = len(nodeTable)            # Count nodes 
     print numNodes
     #for all nodes in text file, set visited to false, distFromSource to infinity & predecessor to none
     **for i in numNodes:
         nodeTable.append(Node(i))**
        #nodeTable.append(Node())

     nodeTable[startNode].distFromSource = 0
     print nodeTable

if __name__ == "__main__":
    populateArray()
    populateNodeTable()

当我运行此代码时,出现以下错误:

    Traceback (most recent call last):
  File "2dArray.py", line 63, in <module>
    populateNodeTable()
  File "2dArray.py", line 18, in populateNodeTable
    for i in numNodes:
TypeError: 'int' object is not iterable

我不确定如何纠正此错误(星号之间的部分) ) - 我想做的是读取我的文本文件,该文件只是一系列用逗号分隔的整数,并计算该文本文件中的节点数 然后,每个节点将被分配 Node 类中的值

I have come across a problem that I don't know how to resolve involving Dijkstra's algorithm - here is my code:

infinity = 1000000
invalid_node = -1
#startNode = 0

class Node:
    distFromSource = infinity
    previous = invalid_node
    visited = False

def populateNodeTable():
     nodeTable = []
     f = open("twoDArray.txt", "r")
     for line in f.readlines(): #get number of nodes from file
       nodeTable.append(line.split(','))   # Create matrix of weights

     numNodes = len(nodeTable)            # Count nodes 
     print numNodes
     #for all nodes in text file, set visited to false, distFromSource to infinity & predecessor to none
     **for i in numNodes:
         nodeTable.append(Node(i))**
        #nodeTable.append(Node())

     nodeTable[startNode].distFromSource = 0
     print nodeTable

if __name__ == "__main__":
    populateArray()
    populateNodeTable()

When I run this code I get the following error:

    Traceback (most recent call last):
  File "2dArray.py", line 63, in <module>
    populateNodeTable()
  File "2dArray.py", line 18, in populateNodeTable
    for i in numNodes:
TypeError: 'int' object is not iterable

I am not sure how I rectify this error (the section between the asterix) - what I am trying to do is to read my text file which is just a series of integers separated by commas, and count the number of nodes within that text file
Each node will then be assigned the values in the Node class

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

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

发布评论

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

评论(3

握住你手 2024-10-25 14:44:19

试试这个:

for i in nodeTable:

为什么要尝试迭代 numNodes?您刚刚将上面的一行定义为表格的长度。

但是在循环中附加到同一个表是没有意义的。并且它不能与读取文件的代码一起工作。而且 Node 类根本不可用......

Try this:

for i in nodeTable:

why are you trying to iterate over numNodes? You just defined one line above as the length of the table.

But appending to the same table in the loop doesn't make sense. And it does not work together with the code that reads the file. Also the Node class isn't usable at all ...

故事灯 2024-10-25 14:44:19

怎么样 for i in range(numNodes) ... numNodes 只是一个数字,而不是一个数字数组,这就是您所追求的。

How about for i in range(numNodes) ... numNodes is just a number, not an array of numbers, which is what you are after.

握住我的手 2024-10-25 14:44:19

如果您想迭代元素索引,请使用 for i, _ in enumerate(nodeTable)

如果您也想访问元素本身,请使用真实名称而不是 _代码>

If you want to iterate over the element indexes, use for i, _ in enumerate(nodeTable)

If you want to access the element itself, too, use a real name instead of _

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