AP Refor
AP Refor

Reputation: 1

How to compare elements in two numpy-arrays and add other element?

I have two numpy arrays with differents lenghts(~ 25k), look like:

a = [['0110000TPK019906K' '325096'] ['0110000TPK01PR12' '225091']...]

b = [['0110000TPK019906K' '4']['0110000TPK01TGTX12K' '5']...]

I need to find all similar elements a[i][0] and b[i][0] and add b[i][1] in array a. Result should be like:

a = [['0110000TPK019906K' '325096' '4']

So I wrote this code and I have some question?

i = 0    
while ( ): # which condition I should use?
        if a[0][i] == b[0][i]:
                quantity = b[0][1]
                np.append(a[0], b[i][1])
        else:
            # how go to next element in array b?

Or, maybe, more effective way exist?

Upvotes: 0

Views: 55

Answers (1)

Sociopath
Sociopath

Reputation: 13426

You can use list-comprehension.

new_array = np.array([[i[0],i[1],j[1]] for i,j in zip(a,b) if i[0]==j[0]])

print(new_array)

Output:

[['0110000TPK019906K' '325096' '4']]

Upvotes: 1

Related Questions