Hello Everyone,
I have a list of digits. What is the most efficient way to count the number of tokens between two known digits?
Here is what I came up with:
def dis(l, a1, a2):
i1 = l.index(a1)
i2 = l.index(a2)
distance = i2 - i1 - 1
return distance
E.g. l = [1, 2, 3, 4, 5]
>>> print dis(l, 1, 5)
3
>>> print dis(l, 1, 3)
1
>>> print dis(l, 3, 4)
0
Is there a better (=faster) way of doing this?
Thank you,
m.