词典列车路线的最佳数据结构? [英] Dictionary best data structure for train routes?
问题描述
所以我基本上读了一个文件(记事本文件),它有一堆火车站和从一站到另一站的时间。例如,它看起来像:
停止A 15
停止B 12
停止C 9
现在我需要返回并访问这些站点及其时间。我正在考虑阅读文件并将其存储为字典。我的问题是,一本字典会是最好的吗?还是有一些其他的python工具可以证明更有用?任何想法都将不胜感激!
我会反对粮食 - 说一个平坦的直觉不是最好的假设你有100个站点和多个不是字母和非数字的路由,那么就是这样。
想想巴黎地铁:
现在尝试使用一个简单的Python dict来计算FDR和La Fourche之间的时间?这涉及两个或多个不同的路线和多个选项。
tree 或某种形式的图是一个更好的结构。对于1到1的映射,一个dict是神话般的;树对于彼此相关的节点的丰富描述更好。然后,您可以使用 Dijkstra's Algorithm 进行浏览。
由于嵌套的词典或列表名词是一个图形,很容易得出一个递归的例子:
def find_all_paths(图,开始,结束,path = []):
path = path + [start]
如果start == end:
返回[路径]
如果开始不在图形中:
返回[]
paths = []
图中的节点[start]:
如果节点不在路径中:
newpaths = find_all_paths(graph,node,end,path)
newpath中的新路径:
paths.append(newpath)
返回路径
def min_path(图,开始,结束):
paths = find_all_paths图,开始,结束)
mt = 10 ** 99
mpath = []
打印'\全部路径:'路径
路径中的路径
t = sum(graph [i] [ j] for i,j in zip(path,path [1 ::]))
print'\t\tevaluating:',path,t
if t< mt:
mt = t
mpath = path
e1 =''.join('{} - > {}:{}'format(i,j,graph [i] [j ])for i,j in zip(mpath,mpath [1 ::]))
e2 = str(sum(graph [i] [j] for i,j in zip(mpath,mpath [1 :: ]))
打印'最佳路径:'+ e1 +'总计:'+ e2 +'\\\
'
如果__name__ ==__main__:
graph = { 'A':{'B':5,'C':4},
'B':{'C':3,'D':10},
'C' D':12},
'D':{'C':5,'E':9},
'E':{'F':8},
'F ':{'C':7}}
min_path(图,'A','E')
min_path(图,'A','D')
min_path 'A','F')
打印:
所有路径:[[ A','C','D','E'],['A','B','C','D','E'],['A','B' ,'E']]
评估:['A','C','D','E'] 25
评估:['A','B','C' D','E'] 29
评估:['A','B','D','E'] 24
最佳路径:A-> B:5 B-& D:10 D-> E:9总计:24
所有路径:[['A','C','D'],['A','B' ''''''''''''''''''''''''''' ','B','C','D'] 20
评估:['A','B','D'] 15
最佳路径:A-> B:5 B - > D:10总计:15
所有路径:[['A','C','D','E','F'],['A','B '''''''''''''''''''''''''''''''''贬低:['A','C','D','E','F'] 33
评估:['A','B','C','D' ,'F'] 37
评估:['A','B','D','E','F'] 32
最佳路径:A-> B: > D:10 D-> E:9 E-> F:8总计:32
So I've been tasked with essentially reading in a file (notepad file) that has a bunch of train stops and the time it takes to get from one stop to another. For example it would look like:
Stop A 15
Stop B 12
Stop C 9
Now I need to go back and access these stops and their times. I was thinking of reading in the file and storing it as a dictionary. My question is, would a dictionary be the best for this? Or is there some other python tool that would prove more useful? Any thoughts would be appreciated!
I will go against the grain -- and say that a straight flat dict is not the best for this.
Let's say you have 100 stops and multiple routes that are non-alphabetical and non-numeric. Think the Paris subway:
Now try and use a straight Python dict to calculate the time between FDR and La Fourche? That involves two or more different routes and multiple options.
A tree or some form of graph is a better structure. A dict is fabulous for a 1 to 1 mapping; tree are better for a rich description of nodes that relate to each other. You would then use something like Dijkstra's Algorithm to navigate it.
Since a nested dict of dicts or dict of lists IS a graph, it is easy to come up with a recursive example:
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if start not in graph:
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
def min_path(graph, start, end):
paths=find_all_paths(graph,start,end)
mt=10**99
mpath=[]
print '\tAll paths:',paths
for path in paths:
t=sum(graph[i][j] for i,j in zip(path,path[1::]))
print '\t\tevaluating:',path, t
if t<mt:
mt=t
mpath=path
e1=' '.join('{}->{}:{}'.format(i,j,graph[i][j]) for i,j in zip(mpath,mpath[1::]))
e2=str(sum(graph[i][j] for i,j in zip(mpath,mpath[1::])))
print 'Best path: '+e1+' Total: '+e2+'\n'
if __name__ == "__main__":
graph = {'A': {'B':5, 'C':4},
'B': {'C':3, 'D':10},
'C': {'D':12},
'D': {'C':5, 'E':9},
'E': {'F':8},
'F': {'C':7}}
min_path(graph,'A','E')
min_path(graph,'A','D')
min_path(graph,'A','F')
Prints:
All paths: [['A', 'C', 'D', 'E'], ['A', 'B', 'C', 'D', 'E'], ['A', 'B', 'D', 'E']]
evaluating: ['A', 'C', 'D', 'E'] 25
evaluating: ['A', 'B', 'C', 'D', 'E'] 29
evaluating: ['A', 'B', 'D', 'E'] 24
Best path: A->B:5 B->D:10 D->E:9 Total: 24
All paths: [['A', 'C', 'D'], ['A', 'B', 'C', 'D'], ['A', 'B', 'D']]
evaluating: ['A', 'C', 'D'] 16
evaluating: ['A', 'B', 'C', 'D'] 20
evaluating: ['A', 'B', 'D'] 15
Best path: A->B:5 B->D:10 Total: 15
All paths: [['A', 'C', 'D', 'E', 'F'], ['A', 'B', 'C', 'D', 'E', 'F'], ['A', 'B', 'D', 'E', 'F']]
evaluating: ['A', 'C', 'D', 'E', 'F'] 33
evaluating: ['A', 'B', 'C', 'D', 'E', 'F'] 37
evaluating: ['A', 'B', 'D', 'E', 'F'] 32
Best path: A->B:5 B->D:10 D->E:9 E->F:8 Total: 32
这篇关于词典列车路线的最佳数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!