Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python映射函数_Python_Dijkstra - Fatal编程技术网

Python映射函数

Python映射函数,python,dijkstra,Python,Dijkstra,我需要一些关于Python映射函数的帮助。我正在尝试执行此代码,但出现了一个错误: 更新后的帖子 这是我的确切代码,以及每个函数的输出: infinity = 1000000 invalid_node = -1 startNode = 0 #Values to assign to each node class Node: def __init__(self): self.distFromSource = infinity self.previous =

我需要一些关于Python映射函数的帮助。我正在尝试执行此代码,但出现了一个错误:

更新后的帖子

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

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)
my 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文档,对于初学者来说,这听起来并不容易

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

print(theNetwork)

它可以是一个列表列表,您希望它成为(为了使用其单个项目作为标记)一个整数列表。

通过在控制台上打印来检查您的网络变量:

print(theNetwork)
for nodeIndex in theNetwork:
它可以是一个列表列表,而您希望它是一个整数列表(以便使用其单个项作为标记)

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

for nodeIndex,node in enumerate(theNetwork):
    # theNetwork[nodeIndex] is now known as node
不使
nodeIndex
迭代网络中的索引,而是迭代其元素的值。你应该使用

for nodeIndex,node in enumerate(theNetwork):
    # theNetwork[nodeIndex] is now known as node

ListofNeighbor是一个从零开始索引的列表。因此,listOfNeighbors[0]=2,listOfNeighbors[1]=4。错误告诉您nodeIndex是一个列表,而不是预期的整数值,您使用它作为索引来访问ListofNeighbor中的项。这意味着网络必须由列表组成。在将值指定给currentDistance之前,可以尝试“打印节点索引”,以查看其组成。此外,我也看不到在上面的函数中定义“node”或“indexNode”的位置


另外,我想澄清一下——这是一个最终将传递给map的函数吗?我在函数的任何地方都看不到映射调用。

listOfNeighbors是一个列表,从零开始索引。因此,listOfNeighbors[0]=2,listOfNeighbors[1]=4。错误告诉您nodeIndex是一个列表,而不是预期的整数值,您使用它作为索引来访问ListofNeighbor中的项。这意味着网络必须由列表组成。在将值指定给currentDistance之前,可以尝试“打印节点索引”,以查看其组成。此外,我也看不到在上面的函数中定义“node”或“indexNode”的位置



另外,我想澄清一下——这是一个最终将传递给map的函数吗?我在函数的任何地方都没有看到映射调用。

网络的内容是什么?这是一个列表列表吗?网络是什么?问题似乎就在那里……网络的内容是什么?我认为错误很明显,比如说你给出的列表
listOfNeighbours
的标记是一个列表而不是一个整数,所以
nodeIndex
是一个列表。网络的内容是什么?这是一个列表列表吗?网络是什么?问题似乎就在那里……网络的内容是什么?我认为错误很明显,比如说你给出的列表
listOfNeighbours
的索引不是一个整数,所以
nodeIndex
是一个列表。我如何发布我的其余代码而不给其他人造成混乱?你可以试着编辑你的代码,使其更加简洁,我的意思是,它至少应该包含每个变量类型的描述。通常,有了更多的背景,人们可以更好地帮助你。第二步是在被问到的时候提供更多的细节:-)好的,我现在已经把所有内容都包括在我的帖子中了,那么print(网络)告诉你什么呢?如果你看到它像:[[1,3],[4,5],…],那么这就是问题所在。您希望网络是一个整数列表,如[1,2,3,4],因此您可以使用它的值作为索引来访问另一个列表。我会检查@phihag的答案,如果这没有帮助,您需要重新考虑访问邻居值列表的方式。基本上现在网络是一个列表列表,你不能用一个列表作为另一个数组的索引。我怎样才能在不引起其他人混乱的情况下发布我的其余代码?你可以尝试编辑你的代码,使它更简洁,我的意思是它至少应该包含每个变量类型的描述。通常,有了更多的背景,人们可以更好地帮助你。第二步是在被问到的时候提供更多的细节:-)好的,我现在已经把所有内容都包括在我的帖子中了,那么print(网络)告诉你什么呢?如果你看到它像:[[1,3],[4,5],…],那么这就是问题所在。您希望网络是一个整数列表,如[1,2,3,4],因此您可以使用它的值作为索引来访问另一个列表。我会检查@phihag的答案,如果这没有帮助,您需要重新考虑访问邻居值列表的方式。基本上,现在网络是一个列表列表,不能将列表用作另一个数组的索引。我在“currentDistance=”上方键入了“print nodeIndex”,但表示没有定义全局名称“node index”。这样做的目的是查看邻居列表,其中应该包含int,并计算出与源的距离,我认为最好使用map,因为它是位于moment@user612041:如果键入了
打印节点索引
,则错误消息不会抱怨
节点索引
。尝试键入正确的名称,并准确报告发生的情况——使用“复制/粘贴”,而不是从内存中键入。
map
函数没有问题,您甚至还没有使用它,所以不要将它包含在问题标题中。我在“currentDistance=”上方键入了“print nodeIndex”,但表示未定义全局名称“node index”。其目的是研究