Python映射函数
我需要一些关于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)
我的网络函数的输出是一个二维列表:
[[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文档中的内容,对初学者来说并没有听起来很简单。
3 个回答
listOfNeighbors 是一个列表,从零开始编号。所以,listOfNeighbors[0] = 2,listOfNeighbors[1] = 4。错误信息告诉你,nodeIndex 这个东西你用来访问 listOfNeighbors 的索引,但它其实是一个列表,而不是我们期待的整数。这意味着 theNetwork 可能是由列表组成的。你可以在给 currentDistance 赋值之前,先用 "print nodeIndex" 来看看它里面有什么。此外,我在上面的函数中也没看到你定义 "node" 或 "indexNode"。
另外,想确认一下——这个函数最终是要传给 map 吗?我在这个函数里没看到任何 map 的调用。
在控制台打印一下你的 theNetwork 变量,看看里面的内容:
print(theNetwork)
它可能是一个列表的列表,而你想要的(为了能把里面的每个项目当作索引使用)是一个整数的列表。
for nodeIndex in theNetwork:
这段话的意思是,nodeIndex
并不是在遍历theNetwork里的索引,而是在遍历它里面元素的值。所以你应该使用
for nodeIndex,node in enumerate(theNetwork):
# theNetwork[nodeIndex] is now known as node