2012-12-04 54 views
-1

我正在編寫計算帆船最快時間的程序。我有一個4x4矩陣,即16個節點。從每個節點我可以走8個不同的方向。我知道從一個大列表中的每個節點到每個方向的旅行時間,所以它現在是8 * 16個字符長。該列表的開頭是這樣的:用字典創建連接節點的圖形

TravelTime = [0.7, 0.5, 10000, 0.5, 0.7, 1.6, 1.3, 1.6, 0.6, 0.5, 0.6, 0.9, 0.6, 0.5, 10000, 10000]... 

,其中前8個數字告訴從Node11所有方向的旅行時間,並在接下來的8號告訴從Node12所有方向的時間。所以它一直持續到Node44的最後信息。 例如TravelTime[0]告訴我時間從Node11直接上升到Node12。現在我需要將它保存到字典中,因此我將其呈現爲:

Graph = { 
'Node11': ['Node12', 0.7], ['Node22', 0.5], ['Node21', 10000], ['Node20', 0.5], ['Node10', 0.7], ['Node00', 1.6], ['Node01', 1.3], ['Node02', 1.6], and then it continues for the next Node: 
'Node12': ['Node13', 0.6], ['Node23', 0.5], ['Node22', 0.6], ['Node21', 0.9], ['Node11', 0.6], ['Node01', 0.5], ['Node02', 10000], ['Node03', 10000] 
} 

'節點'之後的數字等於節點的座標。它從北行開始,順時針方向,總共有8個不同的方向。

SO:我該如何編寫一個構建上述字典的函數?

回答

1

你還沒有提供任何代碼,所以我不打算爲你寫整個解決方案。您輸入的數據是很有條理的,即一切都已經固定長度,所以你可以使用固定長度的循環:

Graph = {} 
for nodeIdx in range(16): 
    Node = {} 
    for directionIdx in range(8): 
     # construct your node list from TravelTime 
    nodeNumber = # Figure out nodeNumber 
    Graph[nodeNumber] = Node 

你應該能夠找出如何挑選基於nodeIdxdirectionIdx從走索引。希望這是一個好的開始。

0

可以說,我們要輸出此格式的詞典:

圖= { 'Node11':[ 'Node12':0.7],[],... []]#分配單個列表值在字典

}

每個鍵和走時是8×16的值的列表。

我們可以粗略地使用這本字典的字符串結構。這可以是一種解決方案:

t = TravelTime 
movements = [(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)] # the order of movements described by you 

output = '{' 
for i in range(1,5): 
     for j in range(1,5): 
       output+="\n'Node"+str(i)+str(j)+"': [" #the key in the dictionary 
       for k in range(8): 
         output+="["+"'Node"+str(i+movements[k][0])+str(j+movements[k][1])+"', "+str(t[((i+j)-2)*8+k])+"]," #the value of dictonary 
       output=output[:-1]+'],' # remove last comma and close the list bracket 
output=output[:-1]+'\n}' #remove last comma and insert last dictionary bracket 

Graph = eval(output) # last step to use Graph as a dictionary 

它將輸出類似的信息(與8倍重複輸入):

{'Node11': [['Node12', 0.7], ['Node22', 0.5], ['Node21', 10000], ['Node20', 0.5], ['Node10', 0.7], ['Node00', 1.6], ['Node01', 1.3], ['Node02', 1.6]], 'Node13': [['Node14', 0.7], ['Node24', 0.5], ['Node23', 10000], ['Node22', 0.5], ['Node12', 0.7], ['Node02', 1.6], ['Node03', 1.3], ['Node04', 1.6]], 'Node12': [['Node13', 0.6], ['Node23', 0.5], ['Node22', 0.6], ['Node21', 0.9], ['Node11', 0.6], ['Node01', 0.5], ['Node02', 10000], ['Node03', 10000]], 'Node14': [['Node15', 0.6], ['Node25', 0.5], ['Node24', 0.6], ['Node23', 0.9], ['Node13', 0.6], ['Node03', 0.5], ['Node04', 10000], ['Node05', 10000]], 'Node24': [['Node25', 0.7], ['Node35', 0.5], ['Node34', 10000], ['Node33', 0.5], ['Node23', 0.7], ['Node13', 1.6], ['Node14', 1.3], ['Node15', 1.6]], 'Node32': [['Node33', 0.6], ['Node43', 0.5], ['Node42', 0.6], ['Node41', 0.9], ['Node31', 0.6], ['Node21', 0.5], ['Node22', 10000], ['Node23', 10000]], 'Node31': [['Node32', 0.7], ['Node42', 0.5], ['Node41', 10000], ['Node40', 0.5], ['Node30', 0.7], ['Node20', 1.6], ['Node21', 1.3], ['Node22', 1.6]], 'Node21': [['Node22', 0.6], ['Node32', 0.5], ['Node31', 0.6], ['Node30', 0.9], ['Node20', 0.6], ['Node10', 0.5], ['Node11', 10000], ['Node12', 10000]], 'Node22': [['Node23', 0.7], ['Node33', 0.5], ['Node32', 10000], ['Node31', 0.5], ['Node21', 0.7], ['Node11', 1.6], ['Node12', 1.3], ['Node13', 1.6]], 'Node23': [['Node24', 0.6], ['Node34', 0.5], ['Node33', 0.6], ['Node32', 0.9], ['Node22', 0.6], ['Node12', 0.5], ['Node13', 10000], ['Node14', 10000]], 'Node33': [['Node34', 0.7], ['Node44', 0.5], ['Node43', 10000], ['Node42', 0.5], ['Node32', 0.7], ['Node22', 1.6], ['Node23', 1.3], ['Node24', 1.6]], 'Node44': [['Node45', 0.7], ['Node55', 0.5], ['Node54', 10000], ['Node53', 0.5], ['Node43', 0.7], ['Node33', 1.6], ['Node34', 1.3], ['Node35', 1.6]], 'Node34': [['Node35', 0.6], ['Node45', 0.5], ['Node44', 0.6], ['Node43', 0.9], ['Node33', 0.6], ['Node23', 0.5], ['Node24', 10000], ['Node25', 10000]], 'Node42': [['Node43', 0.7], ['Node53', 0.5], ['Node52', 10000], ['Node51', 0.5], ['Node41', 0.7], ['Node31', 1.6], ['Node32', 1.3], ['Node33', 1.6]], 'Node43': [['Node44', 0.6], ['Node54', 0.5], ['Node53', 0.6], ['Node52', 0.9], ['Node42', 0.6], ['Node32', 0.5], ['Node33', 10000], ['Node34', 10000]], 'Node41': [['Node42', 0.6], ['Node52', 0.5], ['Node51', 0.6], ['Node50', 0.9], ['Node40', 0.6], ['Node30', 0.5], ['Node31', 10000], ['Node32', 10000]]} 

當然這裏有5以及可能在邊緣達到0的節點。

+0

非常感謝。但問題是我現在將訪問像Node55這樣的節點,其中沒有數據,我不能前往地圖之外的節點。例如,'節點44'是一個角點節點,不能訪問節點43,節點33和節點34以外的其他節點。也許我應該爲你的函數添加一條if語句? – Scirocco

+0

是的,你可以做到這一點。我這樣做是因爲你說你有8 * 16個元素的輸入,因此每個節點總是有8個值,例如,第一個元素是指外部元素。 在邊框上,您可以簡單地指定一個無限值,例如從節點44到節點45。 這個功能對你很有用,只是適應你的輸入;) Ciaoo – caporiccirob