python 函数映射_Python映射函数

本文博主在使用Python map函数和节点表初始化过程中遇到问题,涉及读取网络文件、节点距离赋值和邻接节点查找。详细分析了代码错误,包括类型转换和list操作,旨在帮助初学者理解map函数的正确用法及解决常见问题。
摘要由CSDN通过智能技术生成

我需要一些关于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)

我的网络功能的输出是一个二维列表:

^{pr2}$

我的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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值