2011-03-13 123 views
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 
+0

“我已经改变了我的代码” - 改变了什么?如果这是后续问题,请将链接添加到上一个问题中,以便每个人都能理解上下文。 – MAK 2011-03-13 11:40:49

+0

看来我不能回滚我的编辑以获得我早期的代码。是的,所以我没有对我最近的网络功能进行任何攻击,我也不知道为什么 – user612041 2011-03-13 11:49:21

回答

1

我想你想nodeTable[node],不node[nodeTable],同样有theNetwork[node]