-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:我如何编写一个像上面那样构造字典的函数?

4

2 回答 2

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

您应该能够弄清楚如何根据nodeIdx和从 TravelTime 中选择索引directionIdx。希望这是一个好的开始。

于 2012-12-04T18:45:43.517 回答
0

假设我们想要以这种格式输出字典:

Graph = { 'Node11':[['Node12':0.7],[ ],..., []] # 为字典中的每个键分配一个列表值

}

travelTime 是一个包含 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 个节点可以在边缘到达。

于 2012-12-04T19:43:59.937 回答