0
好的,我已經改變了我的代碼了一點,但是我對變量名應該傳遞給我的nearestNeighbour函數感到困惑。這兩個功能正常工作:確定Dijkstra中的最近鄰居
infinity = 1000000
invalid_node = -1
startNode = 0
#Values to assign to each node
class Node:
def __init__(self):
self.distFromSource = infinity
self.previous = invalid_node
self.visited = False
#read in all network nodes
#node = the distance values between nodes
def network():
f = open ('network.txt', 'r')
theNetwork = [[int(networkNode) for networkNode in line.split(',')] for line in f.readlines()]
#theNetwork = [[int(node) for node in line.split(',')] for line in f.readlines()]
#print theNetwork
return theNetwork
#for each node assign default values
#populate table with default values
def populateNodeTable():
nodeTable = []
index = 0
f = open('network.txt', 'r')
for line in f:
networkNode = map(int, line.split(','))
nodeTable.append(Node())
#print "The previous node is " ,nodeTable[index].previous
#print "The distance from source is " ,nodeTable[index].distFromSource
#print networkNode
index +=1
nodeTable[startNode].distFromSource = 0
return nodeTable
所以,一切都很好。但是,我的下一個功能是給我一個錯誤,儘管我更改了括號中的變量名,但我無法解決這個問題。這是下一個功能代碼和錯誤消息:
def nearestNeighbour(nodeTable, theNetwork):
listOfNeighbours = []
nodeIndex = 0
for networkNode in nodeTable[currentNode]:
if networkNode != 0 and networkNode.visited == False:
listOfNeighbours.append(nearestNode)
nodeIndex +=1
print listOfNeighbours
## #print node.distFromSource, node.previous, node.visited
##
return listOfNeighbours
for networkNode in nodeTable[currentNode]:
TypeError: iteration over non-sequence
「我已經改變了我的代碼」 - 改變了什麼?如果這是後續問題,請將鏈接添加到上一個問題中,以便每個人都能理解上下文。 – MAK 2011-03-13 11:40:49
看來我不能回滾我的編輯以獲得我早期的代碼。是的,所以我沒有對我最近的網絡功能進行任何攻擊,我也不知道爲什麼 – user612041 2011-03-13 11:49:21