Python比较列表并存储在新变量中
我需要一些python方面的帮助 我正在使用Aimsun traffic simulator进行一些模拟,编程语言是python 我的问题是: 我需要搜索车辆可以执行的所有可能路线。 我得到了这些不完整的路线:Python比较列表并存储在新变量中,python,python-3.x,Python,Python 3.x,我需要一些python方面的帮助 我正在使用Aimsun traffic simulator进行一些模拟,编程语言是python 我的问题是: 我需要搜索车辆可以执行的所有可能路线。 我得到了这些不完整的路线: 1. Route 1 - [967, 973] 2. Route 2 - [967, 970] 3. Route 3 - [970, 396] 4. Route 4 - [396, 3269] 5. Route 5 - [3269, 3275] 6. Rou
1. Route 1 - [967, 973]
2. Route 2 - [967, 970]
3. Route 3 - [970, 396]
4. Route 4 - [396, 3269]
5. Route 5 - [3269, 3275]
6. Route 6 - [3275, 3278]
7. Route 7 - [3278, 404]
8. Route 8 - [3278, 408]
9. Route 9 - [404, 448]
10. Route 10 - [408, 410]
11. Route 11 - [408, 411]
12. Route 12 - [448, 454]
13. Route 13 - [410, 419]
14. Route 14 - [410, 433]
15. Route 15 - [410, 437]
16. Route 17 - [411, 412]
起点是967,因此任何车辆都将在此点起动
该列表表示各部分的连接,因此,如果第二列中的值在第一列中的下一个值中没有重复,则该值将表示终点,因此车辆将完成行程
因此,对于finish first路线,我们有:
路线终点1-[967973]
因为973不会在第一列重复
在这种情况下,我使用这个路由存储一个变量
现在问题开始了:
第二条路线由路线2、3、4、5、6组成
Route 2 - [967, 970, 396, 3269, 3275, 3278]
但问题是3278在第一列中重复了两次:
1. Route 7 - [3278, 404]
2. Route 8 - [3278, 408]
所以我有两条路线:
1. Route x - [967, 970, 396, 3269, 3275, 3278, 404]
2. Route y - [967, 970, 396, 3269, 3275, 3278, 408]
问题增加了
1. Route a - [967, 970, 396, 3269, 3275, 3278, 404, 448]
2. Route b - [967, 970, 396, 3269, 3275, 3278, 408, 410]
3. Route c - [967, 970, 396, 3269, 3275, 3278, 408, 411]
4. Route d - [967, 970, 396, 3269, 3275, 3278, 408, 454]
并以同样的方式继续
我如何动态合并这些列表,最后我将所有路由都放在变量中,如:
1. Route Finish 1 - [....]
2. Route Finish 2 - [....]
我尝试了很多代码,比较并存储在变量中,但都不起作用
我希望你们已经理解了。据我所知,你们想找到所有从967开始的、无法延伸的路线 下面是一段代码,从967开始找出最大路径 首先,我们需要输入“不完整路线”列表。 一种可能的路由是这些对的串联
steps = [
[967, 973],
[967, 970],
[970, 396],
[396, 3269],
[3269, 3275],
[3275, 3278],
[3278, 404],
[3278, 408],
[404, 448],
[408, 410],
[408, 411],
[448, 454],
[410, 419],
[410, 433],
[410, 437],
[411, 412],
]
从967年开始,我们延长了路线,保持了所有的可能性。
在这里,我定义了一个函数来增加中间路线的长度
def add_steps(routes, finished_routes):
new_routes = []
for r in routes:
end = r[-1]
r_finished = True
for s in steps:
if s[0] == end:
new_routes.append(r + [s[1]])
r_finished = False
if r_finished:
finished_routes.append(r)
return new_routes, finished_routes
你可以试试
>>> add_steps([[967]], [])
得到
Out: ([[967, 973], [967, 970]], [])
然后,接下来
>>> add_steps([[967, 973], [967, 970]], [])
([[967, 970, 396]], [[967, 973]])
这意味着“[967970396]”是一条可以延伸的中间路线,“[967973]”是一条不能再延伸的成品路线
重复应用此功能,直到完成所有路线
routes = [[967]]
finished_routes = []
for n in range(20):
routes, finished_routes = add_steps(routes, finished_routes)
if not routes:
print finished_routes
break
打印出来的结果是
[[967, 973], [967, 970, 396, 3269, 3275, 3278, 404, 448, 454], [967, 970, 396, 3269, 3275, 3278, 408, 410, 419], [967, 970, 396, 3269, 3275, 3278, 408, 410, 433], [967, 970, 396, 3269, 3275, 3278, 408, 410, 437], [967, 970, 396, 3269, 3275, 3278, 408, 411, 412]]
很抱歉没有使用Python3,因为我没有安装它。请将
print
语句转换为函数,它应该可以工作
概述
根据您的问题陈述,我将您的路线绘制如下:
通过查看此图表,我得出了一些观察结果:
- 如果我从
开始,我将在以下节点(或车站或汽车站)结束:967
,973
,454
,419
,433
和437
。我在代码中调用这些节点412
或leaf\u节点
end\u点
- 您在问题陈述中所称的“路线”只是完整路线的一部分
- 因此,“路由”的数量实际上是
端点中的节点数量
- 对于这个解决方案,我使用python模块。如果您的系统中没有它,则需要安装它。安装超出了本文讨论的范围,但您可以从谷歌搜索python pip开始
- 主要工作是在
函数。此函数用于查看图形、起点和终点,并返回连接起点和终点的所有路径网络中。所有路径都是简单路径()
端点
)端点中的每个节点
,打印从起始节点(967)到该节点的路径967 973
967 970
970 396
396 3269
3269 3275
3275 3278
3278 404
3278 408
404 448
408 410
408 411
448 454
410 419
410 433
410 437
411 412
代码
输出
把它画出来。谷歌“深度优先搜索”和/或“广度优先搜索”。谢谢,工作完美且快速。我对车辆有多少可能的路线选择印象深刻。再次感谢。
# routes.py
# Make sure you have `networkx` installed
import networkx
def build_routes():
'''
Reads a text file, where each line is a pair of nodes, then build
a graph of all the nodes. Returns the graph and a list of leaf nodes
'''
routes = networkx.DiGraph()
leaf_nodes = set()
nonleaf_nodes = set()
with open('routes.txt') as f:
for line in f:
# Each line consists of two nodes: a source and a
# destination.
source, destination = line.split()
nonleaf_nodes.add(source)
leaf_nodes.add(destination)
routes.add_edge(source, destination)
leaf_nodes.difference_update(nonleaf_nodes)
return routes, leaf_nodes
def print_routes(routes, start_point, end_points):
for end_point in end_points:
for path in networkx.all_simple_paths(routes, start_point, end_point):
print ' > '.join(path)
if __name__ == '__main__':
routes, end_points = build_routes()
print_routes(routes, '967', end_points)
967 > 973
967 > 970 > 396 > 3269 > 3275 > 3278 > 404 > 448 > 454
967 > 970 > 396 > 3269 > 3275 > 3278 > 408 > 410 > 419
967 > 970 > 396 > 3269 > 3275 > 3278 > 408 > 411 > 412
967 > 970 > 396 > 3269 > 3275 > 3278 > 408 > 410 > 437
967 > 970 > 396 > 3269 > 3275 > 3278 > 408 > 410 > 433