确定 Dijkstra 中的最近邻
好的,我稍微改变了我的代码,但是我对应该将哪些变量名传递给我的nearestNeighbour 函数感到困惑。这两个功能工作正常:
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
Ok, I have changed my code a little, but I am getting confused on what variable names should be passed to my nearestNeighbour function. These two functions work ok:
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
So, all well and good. However, my next function is giving me an error, and despite me changing variable names in the brackets I can't work out the problem. Here is the next function code and the error message:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要
nodeTable[node]
,而不是node[nodeTable]
,并且与theNetwork[node]
类似。I think you want
nodeTable[node]
, notnode[nodeTable]
, and similarly withtheNetwork[node]
.