题目
9.假设列表 lst_busstop=["龙江新城市","阳光广场","汉江路","嫩江路","清凉山公园""拉萨路”,"五台山”"莫愁路"],存放了某公交线路途经的公交站名。另有字典 dic_estate={"龙江新城市":["白云园”,"腾飞园”],"阳光广场":["龙江小区","芳草园"],"汉江路":["金信花园","龙凤花园”],"嫩江路":["西城蓝湾","花开四季"]},存放了部分公交站附近的小区。试编写程序,根据用户输入的起点(小区名)和终点(小区名),查询公交线路。例如,起点为“白云园”,终点为“龙凤花园”,则输出结果为“起始站:龙江新城市站,终点站:汉江路站共2站路”。若起点为“龙凤花园”,终点为“白云园”,则输出“您需要乘坐反方向线路”。
参考代码
lst_busstop =["龙江新城市","阳光广场","汉江路","嫩江路","清凉山公园","拉萨路","五台山","莫愁路"]
dic_estate = {"龙江新城市":["白云园","腾飞园"],"阳光广场":["龙江小区","芳草园"],
"汉江路":["金信花园","龙凤花园"],"嫩江路":["西城蓝湾","花开四季"]}
start =input("请输入你的起点:")
end =input("请输入你的终点:")
for (i,j) in dic_estate.items():
for z in j:
if z==start:
start_busstop = i
for (i,j) in dic_estate.items():
for z in j:
if z==end:
end_busstop = i
a =lst_busstop.index(start_busstop)
b =lst_busstop.index(end_busstop)
c= b-a
if c>0:
print("起点站:{},终点站:{},共{}站".format(start_busstop,end_busstop,c))
elif c==0:
print("输入错误。")
else:
print("您需要乘坐反方向线路。")
运行结果
请输入你的起点:白云园
请输入你的终点:西城蓝湾
起点站:龙江新城市,终点站:嫩江路,共3站
说明
此方法仅供学习参考,欢迎讨论