Home > Enterprise >  Accessing indices in an array, containes nan elements in a certain range
Accessing indices in an array, containes nan elements in a certain range

Time:07-30

I have an array like:

arr = np.array([[9,9,9,9,9,0,0,0,1,1,1,0,0,9,9],
                [9,9,9,1,1,1,1,0,0,0,0,1,1,1,9],
                [9,1,1,0,0,0,1,1,1,0,0,0,1,1,9],
                [9,1,1,1,1,1,1,1,0,0,0,1,1,1,9]])

I tried with bisect.bisect_right and bisect.bisect_left. Unfortunetely I am not able to do it

Requirement: I Need to access the indices from element value 9 to 9.

required output:

[[5, 6, 7, 8, 9, 10, 11, 12], 
 [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 
 [1, 2, 3, 4, 5, 6, 7, 8,9, 10, 11, 12, 13]]

CodePudding user response:

It's unclear if one can have 9 show up in the middle of the arrays. This will return the desired output:

[list(np.where(i!=9)[0]) for i in arr]
  • Related