Home > Blockchain >  Find the elements have the same value in two numpy arrays python
Find the elements have the same value in two numpy arrays python

Time:11-19

I have two numpy arrays A and B, I want to find in B which rows have the same value (second and third columns) as A. For example, in B I found that 2 & 3 (at the first row) and 8 & 9 (at the 4th row). So I need to print out the row that has the same values which is 0 and 3

A = np.array([[1, 2, 3],            
              [7, 8, 9]]) 

B = np.array([[0, 2, 3],
               [4, 5, 6],
               [4, 5, 6],               
               [1, 8, 9]])

This is my approach:

results = []
for k in range(0,A.shape[0]):
    i = np.where(np.all(np.isclose(B[:,1:3],A[k,1:3], atol=0.5), axis=1))[0]
    #print(i)
    for i in i:
        results.append(i)
print(results)

It can be print out [0, 3] (which is correct)

But when I change A to

A = np.array([[ 111,  67108864,  33554495,  100663297,  50331709,  32,  16777216],
 [ 112,  67108864,  33554475,  100663297,  50331709,  48,  16777248],
 [ 113,  67108864,  33554455,  100663297,  50331709,  16,  16777232],
 [ 114,  67108864,  33554435,  100663297,  50331709,  8,  16777264],
 [ 120,  67108865,  33554455,  100663297,  50331709,  40,  16777224]]) 

And B to

B = np.array([[ 54, 67108864, 33554495, 100663297, 50331711, 0, 16777216],
 [ 55, 67108864, 33554493, 100663297, 50331710, 32, 16777248],
 [ 56, 67108864, 33554491, 100663297, 50331709, 16, 16777232],
 [ 57, 67108864, 33554489, 100663297, 50331708, 48, 16777264],
 [ 58, 67108864, 33554487, 100663297, 50331707, 8, 16777224],
 [ 59, 67108864, 33554485, 100663297, 50331706, 40, 16777256],
 [ 60, 67108864, 33554483, 100663297, 50331705, 24, 16777240],
 [ 61, 67108864, 33554481, 100663297, 50331704, 56, 16777272],
 [ 62, 67108864, 33554479, 100663297, 50331703, 4, 16777220],
 [ 63, 67108864, 33554477, 100663297, 50331702, 36, 16777252],
 [ 64, 67108864, 33554475, 100663297, 50331701, 20, 16777236],
 [ 65, 67108864, 33554473, 100663297, 50331700, 52, 16777268],
 [ 66, 67108864, 33554471, 100663297, 50331699, 12, 16777228],
 [ 67, 67108864, 33554469, 100663297, 50331698, 44, 16777260],
 [ 68, 67108864, 33554467, 100663297, 50331697, 28, 16777244],
 [ 69, 67108864, 33554465, 100663297, 50331696, 60, 16777276],
 [ 70, 67108864, 33554463, 100663297, 50331695, 2, 16777218]]) 

However, I got this results:

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

I don't know why is it!

Any one can explain?

Thank you

CodePudding user response:

Try np.equal instread of np.isclose. Isclose is valid in case where two arrays are element-wise equal within a tolerance so this is why it does not work in your case :

for k in range(0,A.shape[0]):
      i = np.where(np.all(np.equal(B[:,1:3],A[k,1:3]), axis=1))[0]
      for i in i:
          results.append(i)
  • Related