我需要一些Python的map函数帮助。我试图执行此代码,但我收到错误:
更新帖子
这是我的确切代码,以及每个函数的输出:
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
currentNode = startNode
#find the nearest neighbour to a particular node
def nearestNeighbour(currentNode, theNetwork):
listOfNeighbours = []
nodeIndex = 0
for networkNode in theNetwork[currentNode]:
if networkNode != 0 and nodeTable[nodeIndex].visited == False:
listOfNeighbours.append(networkNode)
nodeIndex +=1
print "The nearest neighbours are", listOfNeighbours
## #print node.distFromSource, node.previous, node.visited
##
return listOfNeighbours
def tentativeDistance (theNetwork, listOfNeighbours):
shortestPath = []
for nodeIndex in theNetwork:
currentDistance = listOfNeighbours[nodeIndex] + startNode
print currentDistance
if currentDistance[theNetwork][nodeIndex] < Node.distFromSource:
theNetwork[node].previous = nodeIndex
theNetwork[node].distFromSource = nodeIndex
theNetwork[node].visited = True;
shortestPath.append(indexNode)
nodeIndex +=1
print shortestPath
if __name__ == "__main__":
nodeTable = populateNodeTable()
#nodeTable = populateNodeTable(self)
theNetwork = network()
#listOfNeighbours = nearestNeighbour(currentNode, theNetwork)
#tentativeDistance(theNetwork, listOfNeighbours)
我的网络功能输出是一个2D列表:
[[0, 2, 4, 1, 6, 0, 0], [2, 0, 0, 0, 5, 0, 0], [4, 0, 0, 0, 5, 5, 0], [1, 0, 0, 0, 1, 1, 0], [6, 5, 0, 1, 0, 5, 5], [0, 0, 5, 1, 5, 0, 0], [0, 0, 0, 0, 5, 0, 0]]
我的populateNodeTable函数的输出是:
The previous node is -1
The distance from source is 1000000
The previous node is -1
The distance from source is 1000000
The previous node is -1
The distance from source is 1000000
The previous node is -1
The distance from source is 1000000
The previous node is -1
The distance from source is 1000000
The previous node is -1
The distance from source is 1000000
The previous node is -1
The distance from source is 1000000
我的网络文本文件具有此格式(减去行间距):
0,2,4,1,6,0,0
2,0,0,0,5,0,0
4,0,0,0,5,5,0
1,0,0,0,1,1,0
6,5,0,1,0,5,5
0,0,5,1,5,0,0
0,0,0,0,5,0,0
错误是:
currentDistance = listOfNeighbours[nodeIndex] + startNode
TypeError: list indices must be integers, not list
这是我的listOfNeighbours的内容,在我的另一个函数中生成:
[2, 4, 1, 6]
我不明白这方面的Python文档,对初学者来说听起来并不容易
答案 0 :(得分:4)
for nodeIndex in theNetwork:
不会使nodeIndex
遍历theNetwork
中的 indices ,而是覆盖其元素的 values 。你应该使用
for nodeIndex,node in enumerate(theNetwork):
# theNetwork[nodeIndex] is now known as node
答案 1 :(得分:0)
通过在控制台上打印来检查您的网络变量:
print(theNetwork)
它可以是一个列表列表,你想要它(为了使用它的各个项目作为标记)是它的一个列表。
答案 2 :(得分:0)
listOfNeighbors是一个列表,从零开始索引。所以,listOfNeighbors [0] = 2,listOfNeighbors [1] = 4.错误告诉你nodeIndex,你用作索引来访问listOfNeighbors中的项目,是一个列表 - 不是预期的整数值。这意味着网络必须由列表组成。在为currentDistance分配值之前,您可以尝试“print nodeIndex”以查看它的组成。另外,我也看不到你在上面的函数中定义“node”或“indexNode”的位置。
另外,只是为了澄清 - 这是一个你最终将传递给地图的功能吗?我没有在函数中的任何地方看到地图调用。