for rout in range(1,6):
print 'From: '+str(int(s_dict[rout]['Origin']))+','+' to: '+str(int((s_dict[rout]['Destination'])))+','+' Stops: '+(s_dict[rout]['Stops'])+','+' Cost: '+(s_dict[rout]['Cost'])+','+' Time: '+(s_dict[rout]['Time'])
print 'All routes:'
for n in range(len(all_path[rout-1])):
all_routs=''
for s in range(len(all_path[rout-1][n])):
all_routs+= str(all_path[rout-1][n][s])
stops=str(len(all_routs)-2)
cost=0
for trips in range(len(sec)):
if sec[trips][X]==(all_path[rout-1][n][0]) or sec[trips][X]==(all_path[rout-1][n][1]):
cost+=sec[trips][3]
print '->'.join(all_routs)+', Stops: '+stops+', Cost: '+str(cost)
X索引不是代码的一部分,因为它是导致问题的原因,我找不到正确的索引方法
代码的目的是从s_dict
获取“请求”并将其与main_dict
的旅行信息相匹配。在s_dict[0]
中,客户希望从Origin
'2'转到Destination
'5',Cost
为0,这意味着价格无关紧要,{{1} 1}},Time
是99,这也意味着它有多少并不重要。
现在我应该找到从'2'到'5'的所有可用路径并返回每个成本/消耗时间的数量
来自s_dict = {
1:{'Origin':'002','目的地':'005','费用':'0000.00','停止':'99','时间':'00 .00'},
2:{'Origin':'002','目的地':'005','费用':'0000.00','停止':'11','时间':'00 .00'},
3:{'Origin':'002','目的地':'005','费用':'1450.11','停止':'99','时间':'00 .00'},
4:{'Origin':'004','目的地':'005','费用':'1550.11','停止':'99','时间':'22 .22'},
5:{'Origin':'001','目的地':'005','费用':'0000.00','停止':'99','时间':'11 .00'}}
main_dict =
{1:{'Origin':'001','Destination':'002','Cost':'0100.00','Time':'04 .00'},
2:{'Origin':'002','Destination':'003','Cost':'0500.00','Time':'01 .50'},
3:{'Origin':'002','Destination':'004','Cost':'0700.00','Time':'10 .00'},
4:{'Origin':'002','Destination':'005','Cost':'1500.00','Time':'05 .75'},
5:{'Origin':'003','Destination':'004','Cost':'0200.00','Time':'11 .40'},
6:{'Origin':'004','Destination':'005','Cost':'0750.00','Time':'10 .50'},
7:{'Origin':'004','Destination':'006','Cost':'0550.00','Time':'06 .75'}}
Stops
的我将信息删除了,因为我更容易以这种方式使用它并制作main_dict
秒= [
[1,2,4.0,100.0],
[2,3,1.5,500.0],
[2,4,10.0,700.0],
[2,5,5.75,1500.0],
[3,4,11.4,200.0],
[4,5,10.5,750.0],
[4,6,6.75,550.0]]
all_path = [
[[2,3,4,5],[2,4,5],[2,5]],
[[4,5]],
[[1,2,3,4,5],[1,2,4,5],[1,2,5]]]
sec
来自all_path
它在三个第一种情况下需要2个特定值,它是2-> 5,这意味着我想从原点2到目的地5,我应该显示每个可用的路径,这意味着2-> 3-> 4- > 5,2-> 4-> 5,2-> 5
这就是道路,现在我正试图获得每次旅行的费用。
换句话说,如果我们取2→4-> 5然后在s_dict中它将2作为原点而3作为目的地,超出s_dict
变量中的成本值然后取3作为原点和4作为目的地,并将成本与cost
变量相加。
我的问题在于这里的索引cost
问题主要是如何索引X“X不是代码的一部分”
我已经尝试了很多方法来解决它,但它不起作用,我得到的最好的是它改变了目的地并始终保持相同的原点,因此2-> 3 + 2-> 4 + 2-的成本> 5而不是2-> 3 + 3-> 4 + 4-> 5
答案 0 :(得分:2)
在评论中经过漫长的漫游,我不认为这可以解决您的特定问题。我假设您在到达costs=99
时已经解决了all_path
的问题。在这种情况下,我认为你应该将你的代码重构为类似下面的代码,以摆脱你自己所处的索引。它绝不是完美的,但希望更容易理解。
import random
import itertools
###### Generate some fake data for our dict ######
# First get all of our location pairings as tuples
loc_pairs = list(itertools.product(range(1, 6), range(1, 6)))
# Build cost dictionary. Tuple key is (from-location, to-location)
cost_dict = {}
for loc_pair in loc_pairs:
cost_dict[loc_pair] = {'cost': random.randint(0, 50),
'time': random.randint(0, 50)}
##### Now your data for paths ######
all_path=[[[2, 3, 4, 5], [2, 4, 5], [2, 5]], [[4, 5]], [[1, 2, 3, 4, 5],
[1, 2, 4, 5], [1, 2, 5]]]
### Build the printout
for start_location in all_path:
for route in start_location:
locations_visited = ' -> '.join(str(item) for item in route)
costs = 0
times = 0
try:
for x in range(len(route)-1):
costs += cost_dict[(route[x], route[x+1])]['cost']
times += cost_dict[(route[x], route[x+1])]['time']
print("The route: {}".format(locations_visited))
print("costs: {}".format(costs))
print("took: {}".format(times))
except:
pass
假设您的main_dict
数据结构准确无误,您可以使用以下内容构建实际成本字典:
real_costs = {1: {'Origin': '001', 'Destination': '002', 'Cost': '0100.00', 'Time': '04.00'},
2: {'Origin': '002', 'Destination': '003', 'Cost': '0500.00', 'Time': '01.50'},
3: {'Origin': '002', 'Destination': '004', 'Cost': '0700.00', 'Time': '10.00'},
4: {'Origin': '002', 'Destination': '005', 'Cost': '1500.00', 'Time': '05.75'},
5: {'Origin': '003', 'Destination': '004', 'Cost': '0200.00', 'Time': '11.40'},
6: {'Origin': '004', 'Destination': '005', 'Cost': '0750.00', 'Time': '10.50'},
7: {'Origin': '004', 'Destination': '006', 'Cost': '0550.00', 'Time': '06.75'}}
real_cost_dict = {}
for key, value in real_costs.items():
pairing = (value.get('Origin'), value.get('Destination'))
real_cost_dict[pairing] = {'cost': value.get('Cost'),
'time': value.get('Time')}
答案 1 :(得分:0)
如果我已正确理解代码,那么基本上需要的是某种功能。
基本上:
def path_finding_algorithm('[INPUT]'):
//loads of processing here
//
// '[PROCESS]'
//
return '[OUTPUT]'
您仅向我们提供了['OUTPUT']
以及['PROCESS']
的缩短版本。但我们必须知道['INPUT']
是什么以及['PROCESS']
正在尝试做什么
否则,没有足够的信息来回答您的问题。
一个好的起点是陈述情况:
我正在为导航应用程序设计路径查找算法。我使用成本为距离的网格地图来做到这一点 顶点之间。这是我的成本函数:
//代码//
有没有办法优化它?