Reputation: 3
In the problem I am currently solving involves buses so that will hopefully clear up the mention of stops and routes in the code.
So in essence what I am trying to achieve with this function is to get the next stop given a stop. I have gotten roughly 80% of the way through this problem but am stuck on getting the next stop
def nextstop(stops,routes,stopX,routeX):
matchedRoute = []
matchedstop = []
for i in routes:
if routeX in i:
matchedRoute = i
for i in matchedRoute[1]:
if i == stopX[0]:
matchedstop = next(iter(matchedRoute))
print(matchedstop)
So let's say I am operating over this tuple:
('Route 5', [1118, 1114, 1115, 533, 1370, 1091, 2363, 1296, 1298, 763, 852, 851, 995, 815, 814, 848, 846, 845, 842, 841, 838, 822, 819, 818, 997, 996, 767, 622, 621, 620, 1082])
and what I have is the stop that I want to match against for example [1115[. What I need somehow is to return the next stop [533].
but my code returns the beginning of the tuple 'Route 5'
This is where I'm having trouble how would I go against getting the next element in the sequence?
EDIT : Clarifying as asked
In Ipython I'd parse the function as such
python nextstop(stops,routes,stops[533],'Route 5')
The first loop takes a huge list of tuples I have in a csv file and iterates through them pattern matching against the names of the routes. When it does match it stores that tuple as matchedRoute.
I then am trying to iterate through the matchedRoute to find where the stop is in that route and I need to retrieve the next stop based off that.
Upvotes: 0
Views: 129
Reputation: 15872
You can do this:
def nextstop(stops,routes,stopX,routeX):
matchedRoute = []
matchedstop = []
for i in routes:
if routeX in i:
matchedRoute = i
if stopX[0] in matchedRoute[1]:
if matchedRoute[1].index(stopX[0]) == len(matchedRoute[1]):
matchedstop = "This is the last stop"
else:
matchedstop = matchedRoute[1][matchedRoute[1].index(stopX[0])+1]
else:
matchedstop = "Stop not found in route"
print(matchedstop)
I can not test this exact code as I do not know the input, but I can give you an example how it works:
tup1 = ('Route 5', [1118, 1114, 1115, 533, 1370, 1091, 2363, 1296, 1298, 763, 852, 851, 995, 815, 814, 848, 846, 845, 842, 841, 838, 822, 819, 818, 997, 996, 767, 622, 621, 620, 1082])
route = 'Route 5'
stop = 1115
r_x, s_x = tup1
if r_x == route:
if stop in s_x:
print(s_x[s_x.index(stop)+1])
Output:
533
list.index(elem)
method returns the index of the first occurrence of the element in the list. So if you access the next index, i.e. by adding one to it, you can get the next index. That way you do not need to loop over the stops.
EDIT:
#Example:
>>> routes = [('Route 5', [1118, 1114, 1115, 533, 1370, 1091, 2363, 1296, 1298, 763, 852, 851, 995, 815, 814, 848, 846, 845, 842, 841, 838, 822, 819, 818, 997, 996, 767, 622, 621, 620, 1082]),('Route 4', [1118, 1114, 1115, 533, 1370, 1091, 2363, 1296, 1298, 763, 852, 851, 995, 815, 814, 848, 846, 845, 842, 841, 838, 822, 819, 818, 997, 996, 767, 622, 621, 620, 1082])]
>>> nextstop(1,routes,[1115],'Route 5')
533
EDIT 2: A shorter answer would be:
def nextstop(stops,routes,stopX,routeX):
for route, stop in routes:
if route == routeX:
if stopX[0] in stop:
try:
print(stop[stop.index(stopX[0])+1])
Except IndexError:
print("This is the last stop")
else:
print("Stop not found")
Upvotes: 1