ダイクストラのアルゴリズムを含む解決方法がわからないという問題に遭遇しました-これが私のコードです:
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 クラスの値が割り当てられます