首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python函数

Python函数
EN

Stack Overflow用户
提问于 2011-03-13 17:44:07
回答 3查看 1.7K关注 0票数 0

在Python的map函数方面,我需要一些帮助。我正在尝试执行这段代码,尽管我得到了一个错误:

更新后

这是我的确切代码,以及每个函数的输出:

代码语言:javascript
复制
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列表:

代码语言:javascript
复制
[[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函数的输出是:

代码语言:javascript
复制
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

2,0,0,0,5,0,0

4,0,0,0,5,5,0

1,0,0,0,1,0

6,5,0,1,0,5

0,0,5,1,5,0,0

0,0,0,0,5,0,0

错误是:

代码语言:javascript
复制
currentDistance = listOfNeighbours[nodeIndex] + startNode
TypeError: list indices must be integers, not list

这是在我的另一个函数中生成的listOfNeighbours的内容:

代码语言:javascript
复制
[2, 4, 1, 6]

我不理解Python文档,对于初学者来说,这听起来并不容易

EN

回答 3

Stack Overflow用户

发布于 2011-03-13 17:48:17

代码语言:javascript
复制
for nodeIndex in theNetwork:

不使nodeIndex迭代theNetwork中的索引,而是使其迭代其元素的值。你应该用

代码语言:javascript
复制
for nodeIndex,node in enumerate(theNetwork):
    # theNetwork[nodeIndex] is now known as node
票数 4
EN

Stack Overflow用户

发布于 2011-03-13 17:47:21

通过在控制台上打印theNetwork变量来检查它:

代码语言:javascript
复制
print(theNetwork)

它可以是一个列表,您希望它是什么(为了使用它的个别项目作为指标)是一个ints的列表。

票数 0
EN

Stack Overflow用户

发布于 2011-03-13 17:58:08

listOfNeighbors是一个列表,索引从零开始。因此,listOfNeighbors = 2,listOfNeighbors1 = 4。错误告诉您,nodeIndex是一个列表,而不是预期的整数值,您正在使用nodeIndex作为索引来访问listOfNeighbors中的项。这意味着theNetwork必须由列表组成。在向nodeIndex分配值之前,您可以尝试使用"print currentDistance“来查看它由什么组成。另外,我也看不出在上面的函数中定义“节点”或"indexNode“的位置。

另外,为了澄清--这是你最终要传递给地图的函数吗?我没有看到函数中任何地方都有地图调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5291182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档