Say I have an array of degree values, like this:
DEGREES = [
0, 15, 30, 45, 60,
75, 90, 105, 120,
135, 150, 165, 180,
195, 210, 225, 240,
255, 270, 285, 300,
315, 330, 345,
]
I would pick an angle and then be able to bisect this hypothetical circle in order to make it easier to find the shortest route to the target direction.
Saying that, how can I pick a specific value, like 90
, and then be able to find the previous 12 elements behind that, including the index wrapping around to the end?
So, taking that earlier value and applying to that list, I would get something like this:
[90, 75, 60, 45, 30, 15, 0, 345, 330, 315, 300, 285, 270]
Using slice notation, I tried doing this:
index = DEGREES.index(90)
print(DEGREES[index-12:index]) # start 12 values back, stop at index
But this only prints an empty array.
Is there a way to slice a list so I can get the 12 previous values behind the index I'm using?
EDIT:
This turned out to be an XY Problem, my bad. Originally, I was trying to create a smooth rotation system in Pygame, with my attempts to calculate angles not working, I asked this question to solve a problem with yet another idea I was trying to implement. I ended up accepting the answer that helped me set up the smooth rotation system, but there are relevant answers to the original question below that.
grab the index of an arbitrary element and the values of its neighbors
. Which is a clearly different question – BallmanDEGREES
just an example or will you only use this slicing with increasing values and a constant increment? – Hagerrange(0,360,15)
and you could calculate the information you're after on the fly:[i%360 for i in range(90,90-15*13,-15)]
– Recor