in my program I want to have 1D array, convert it to a 2D array, convert it back to a 1D array again and I want to search for a value in the final array. In order to change 1D array to 2D, I used numpy. I used where() function in order to search through the array and in the end I get this output:
(array([4], dtype=int32),)
I get this result but I only need its index, 4 in case. Is there a way which I can only get the numerical result of the where() function or is there an alternative way which allows me to do 1D to 2D and 2D to 1D conversions without using numpy?
import numpy as np
a = [1,2,3,4,5,6,7,8,9];
print(a)
b = np.reshape(a,(3,3))
print(b)
c = b.ravel()
print(c)
d = np.where(c==5)
print(d)
wheregives you a tuple of arrays, one array per dimension. Applied to a 1d array, it is a 1 element tuple; applied to the 2d array it is a 2 element tuple.d[0]pulls that array out of the tuple. By the way,dcan be used as an index, e.g.a[d]works just as well asa[4].np.searchsorted(c, 5)print(dir(d))this will tell you what attributes are availablenp.argmax(a==5)will give you the index of the first5in the array.5in a.