Home > Blockchain >  Numpy find index in a list of arrays appeared in another list of arrays
Numpy find index in a list of arrays appeared in another list of arrays

Time:05-21

So suppose I have a list of arrays called original_arr, another list of arrays called find_arr,how can I find the index of elements of original_arr appeared in find_arr? (I have many number of lists to deal with)

a = np.arange(27).reshape(9,3)
original_arr = np.split(a,3)
>>> original_arr
>>> [array([[0, 1, 2],
            [3, 4, 5],
            [6, 7, 8]]), 
     array([[ 9, 10, 11],
            [12, 13, 14],
            [15, 16, 17]]), 
     array([[18, 19, 20],
            [21, 22, 23],
            [24, 25, 26]])]

b = np.array([[0,1,2],[3,4,5],[12,13,14],[15,16,17],[21,22,23]])
find_arr =  np.split(b,np.array([2,2,1]).cumsum()[:-1])
>>> find_arr
>>> [array([[0, 1, 2],
            [3, 4, 5]]), 
     array([[12, 13, 14],
            [15, 16, 17]]), 
     array([[21, 22, 23]])]

so I tried the code below, but it doesn't work

index = np.empty((0,2),int)

for i in range (len(find_arr)):
    for j in range (len(find_arr[i])):
        m = np.where((original_arr[i] == find_arr[i][j]).all(axis=1))
        index = np.append(index,m,axis=0)

what I expect the output is to be (for example. because [0, 1, 2] in find_arr appears in original_arr[0][0], so the output should be [0,0] or [[0],[0]]) :

array[[[0,0],[0,1]],
      [[1,1],[1,2]],
      [[2,1]]]

or 
array[[[[0],[0]],[[0],[1]]],
      [[[1],[1]],[[1],[2]]],
      [[[2],[1]]]]

can anybody check where my code is wrong? or is there any easier way to do this?

CodePudding user response:

You want to append the row number, i, to m, but m has different type. So take the output from np.where(..)[0][0] as below and append it to i before inserting into index.

for i in range (len(find_arr)):
    for j in range (len(find_arr[i])):
        m = np.where((original_arr[i] == find_arr[i][j]))[0][0]
        index = np.append(index,[[i, m]],axis=0)

Another option, which might be a bit faster, is to use np.nonzero(..==..)[0][0] like this:

m = np.nonzero((original_arr[i] == find_arr[i][j]))[0][0]

Both of which will output:

[[0 0]
 [0 1]
 [1 1]
 [1 2]
 [2 1]]

as you would expect.

  • Related