I have a list of points positions, as tuples, and I'm trying to collect the points before and after point N.
points = [
(0, 0),
(1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 5),
(6, 6),
]
Eg. If I have point three (3, 3)
as an input the expected output is [(2,2), (4,4)]
.
Testing this out, it works fine:
index = 3
a, n, b = points[ index-1 : index+2 ]
print(a, b)
# Returns: (2, 2) (4, 4)
As long as the index is not less than 2 or greater than 5 it works as expected.
Here follows a few examples of where the list of tuples acts as expected:
# Tuple at index zero
print(points[0])
# Returns: (0, 0)
# Tuples from index zero to (but not including) index three
print(points[0:3])
# Returns: [(0, 0), (1, 1), (2, 2)]
# Tuple at index negative one, (6,6) in this case
print(points[-1])
# Returns: (6, 6)
However, when I combine the negative and positive indices it falls apart, why?
# Tuples from index negative one, up to but not including index two
print(points[-1:2])
# Returns: []
The expected output is [(6, 6), (0, 0), (1, 1)]
while python gives []
.
Solution
Thanks to Dev Khadka.
points = [
(0,0),
(1,1),
(2,2),
(3,3),
(4,4),
(5,5),
(6,6),
]
index = 0
a, _, b = np.roll(points, -(index-1), axis=0)[:3]
sel_points = zip(a,b)
print(list(zip(*sel_points)))
Result: [(6, 6), (1, 1)]