0

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のドキュメントを理解していません。初心者にとっては簡単に聞こえません

4

3 に答える 3

4
for nodeIndex in theNetwork:

のインデックスではなく、その要素の値を反復nodeIndex処理します。使用する必要がありますtheNetwork

for nodeIndex,node in enumerate(theNetwork):
    # theNetwork[nodeIndex] is now known as node
于 2011-03-13T17:48:17.670 に答える
0

コンソールに表示して、theNetwork 変数を確認します。

print(theNetwork)

それはリストのリストである可能性があり、(個々の項目を指標として使用するために) それを int のリストにする必要があります。

于 2011-03-13T17:47:21.850 に答える
0

listOfNeighbors は、ゼロからインデックス付けされたリストです。したがって、listOfNeighbors[0] = 2、listOfNeighbors[1] = 4. エラーは、listOfNeighbors 内のアイテムにアクセスするためのインデックスとして使用している nodeIndex がリストであることを示しています。期待どおりの整数値ではありません。つまり、theNetwork はリストで構成されている必要があります。currentDistance に値を割り当てる前に、「print nodeIndex」を試して、その構成を確認できます。また、上記の関数で「node」または「indexNode」を定義する場所もわかりません。

また、明確にするために、これは最終的にマップに渡す関数ですか? 関数のどこにも map 呼び出しがありません。

于 2011-03-13T17:58:08.647 に答える