Logic Behind Problem: When building a route you need two steps in order to perform a direction. For example if you go from Step1 to Step2 that is right. But Step1 to Step3 is left. The steps need to be in pairs in order to determine direction.
Issue:
I have a function that produces a 'Direction'
based on a pair of elements in 'StepName'
. If a routes 'StepName'
pair is already defined with 'Direction'
it should be able to produce the same 'Direction'
in another route dictionary if it contains the same pair of elements in a sequence of pairs. Doesn't matter at what index the pair is located in the list
For example:
104-1 to 104-2
produces'Left'
in'Direction'
104-2 to 105-A
produces'Right'
in'Direction'
105-A to 105-D
produces'Right'
in'Direction'
Code:
route1 = {
'RouteID': 1,
'StepID': [1, 2, 3, 4],
'StepName': ['104-1', '104-2', '105-A', '105-D'],
'Direction': ['Left', 'Right', 'Right']}
route2 = {
'RouteID': 2,
'StepID': [1, 2, 3, 4],
'StepName': ['104-2', '105-A', '105-C', '105-B'],
'Direction': []}
def routeMapper(longRoute, subRoute):
for i, v in enumerate(subRoute['StepName']):
found = False
for j, b in enumerate(longRoute['StepName']):
if v == b:
found = True
subRoute['Direction'].append(longRoute['Direction'][j])
if not found:
subRoute['Direction'].append(False)
routeMapper(route1, route2)
print(route2)
Output:
{'RouteID': 2, 'StepID': [1, 2, 3, 4], 'StepName': ['104-2', '105-A', '105-C', '105-B'], 'Direction': ['Right', 'Right', False, False]}
As you can see it is comparing what each index element direction it is assigned to. I want it to be seen in pairs instead of single.
Desired Output: (for 'Direction')
{'RouteID': 2, 'StepID': [1, 2, 3, 4], 'StepName': ['104-2', '105-A', '105-C', '105-B'], 'Direction': ['Right', False, False, False]}
The reason this output makes sense is because in route1
104-2 to 105-A
produces 'Right'
even though it is 1
and 2
in the index as a pair compared to it being at the 0
and 1
index in route2
. As long as the pair is found in the list it should be able to produce the direction.