Reputation: 89
Let's say I have a really long array like this:
arr = np.array([0,0,1,1,0,1,1,1,0,0,0,1,1,0,0,1]}
I also have a maximum position where I can look, like 6, making a slice of the array that returns this:
sliceLocation = 6
returning:
np.array([0,0,1,1,0,1,1]}
now I want to write a piece of code that slices the array and then searches for the first 0 inside this array coming from the back, in this case returning 4
as a viable position in a quick and efficient time. Can anybody help me?
Upvotes: 0
Views: 185
Reputation: 10624
The following should work:
sliceLocation = 6
sliceLocation-list(arr)[:sliceLocation+1][::-1].index(0)
4
How it works: It slices the array, changes it to a list, reverses it, find the index of first 0 and by subtracting it from sliceLocation it gives the final result
Upvotes: 0
Reputation: 86
Here is your example but i dont understand what you want to do with the rest of the array arr.
arr = np.array([0,0,1,1,0,1,1,1,0,0,0,1,1,0,0,1])
sliceLocation = 6
arr = arr[:sliceLocation+1]
idx = np.where(arr==0)[0][-1]
idx = 4
Upvotes: 1