別の関数にリストを入力できるように、関数から引数を渡す方法を理解する方法に苦労しています-私のコードは次のとおりです:
infinity = 1000000
invalid_node = -1
startNode = 0
#Values to assign to each node
class Node:
distFromSource = infinity
previous = invalid_node
visited = False
#read in all network nodes
def network():
f = open ('network.txt', 'r')
theNetwork = [[int(node) for node in line.split(',')] for line in f.readlines()]
print theNetwork
return theNetwork
#for each node assign default values
def populateNodeTable():
nodeTable = []
index = 0
f = open('network.txt', 'r')
for line in f:
node = map(int, line.split(','))
nodeTable.append(Node())
print "The previous node is " ,nodeTable[index].previous
print "The distance from source is " ,nodeTable[index].distFromSource
index +=1
nodeTable[startNode].distFromSource = 0
return nodeTable
#find the nearest neighbour to a particular node
def nearestNeighbour(currentNode, theNetwork):
nearestNeighbour = []
nodeIndex = 0
for node in nodeTable:
if node != 0 and currentNode.visited == false:
nearestNeighbour.append(nodeIndex)
nodeIndex +=1
return nearestNeighbour
currentNode = startNode
if __name__ == "__main__":
nodeTable = populateNodeTable()
theNetwork = network()
nearestNeighbour(currentNode, theNetwork)
そのため、最も近いNeighbour関数の最も近いNeighbourリストに、他のノードに最も近いノードのリストを入力しようとしています。これで、他のすべての関数が正しく機能し、すべての引数の受け渡しが正常に機能します。ただし、私のneastNeighbour関数は次のエラーメッセージをスローします。
ノード!=0およびtheNetwork[currentNode] .visited == falseの場合:AttributeError:'list'オブジェクトに属性'visited'がありません
(レイアウトについてお詫びしますが、コード引用符の使用についてはまだ十分に理解していません)