我如何从一个函数摸出我如何传递参数,这样我可以在另一个函数填充列表挣扎 - 我的代码是:Python的 - 传递函数参数
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)
所以,我想使用距离其他节点最近的节点列表填充最近的邻近函数中的最近的邻近列表。现在,所有其他函数都可以正常工作,并且所有参数都按照它的原样传递。 然而,我nearestNeighbour函数抛出了此错误消息:
if node != 0 and theNetwork[currentNode].visited == false: AttributeError: 'list' object has no attribute 'visited'
(道歉布局,还没有完全领悟了使用该代码的报价还)
啊好吧,我已经试过了,我得到一个错误信息 - 这可能与我有什么了工作,但只是改变我的nearestNeighbour代码功能? – user612041 2011-03-11 00:39:20