python 函式對映 Python對映函式

2021-10-18 14:01:09 字數 3404 閱讀 7970

我需要一些關於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(','))

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:

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;

nodeindex +=1

print shortestpath

if __name__ == "__main__":

nodetable = populatenodetable()

#nodetable = populatenodetable(self)

thenetwork = network()

#listofneighbours = nearestneighbour(currentnode, thenetwork)

#tentativedistance(thenetwork, listofneighbours)

我的網路功能的輸出是乙個二維列表:

^$我的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文件中關於這方面的內容,這對初學者來說並不容易

python簡述定義函式的規則 python 函式

一.定義函式 函式是組織好的,可重複使用的,用來實現單一,或相關聯功能的 段 函式能提高應用的模組性,和 的重複利用率 函式式 將某功能 封裝到函式中,日後便無需重複編寫,僅呼叫函式即可 物件導向 對函式進行分類和封裝,讓開發 更快更好更強.函式式程式設計最重要的是增強 的重用性和可讀性 1.定義函...

pytho函式星號引數

神奇的星號 傳遞實參和定義形參 所謂實參就是呼叫函式時傳入的引數,形參則是定義函式是定義的引數 的時候,你還可以使用兩個特殊的語法 呼叫函式時使用 test args 的作用其實就是把序列 args 中的每個元素,當作位置引數傳進去。比如上面這個 如果 args 等於 1,2,3 那麼這個 就等價於...

python class函式報錯 Python類

建立乙個類 class person pass p person print p 類以class 宣告 self變數 這個定義與c 的this指標,都是指向物件示例本身,但使用起來是不一樣的。可以看到函式定義的時候,都要帶乙個self引數。這是因為python直譯器會預設將物件本身作為引數傳入函式。...