2

I am using some datas in my program were I have some sorting issue which takes longer time for me. So I have mentioned an example situation here for which I would like to get a solution.

import numpy as np 
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
B = np.array([[4,5,6,7],[7,8,9,4],[1,2,3,2]])
# Need to apply some sort function
C = sort(B[:,0:3] to be sorted with respect to A)
print(C)

I have two numpy arrays were I would like the first 3 columns of array B to be sorted with respect to array A.

And I want the output of C as

[[1,2,3,2],[4,5,6,7],[7,8,9,4]]

Is there any numpy method or any other python libraries which could do this.

Looking forward for some answers

Regards

Aadithya

3
  • 1
    stackoverflow.com/questions/9007877/… Commented Jun 8, 2020 at 11:11
  • Would C be the same if A was inversed? Commented Jun 8, 2020 at 11:17
  • @tstanisl .. A is my input data I read from a csv file. So I wouldn't inverse it. Using A I create some output datas which I add as a 4th column . So now my B variable has columns of A + a column of my output data (as shown) . But the rows (upto first 3 columns) in B are shuffled. So I would like to reorder the first 3 coloumns of B with respect to A. Meanwhile the 4th coloumn should accordingly get sorted to its respective row . I hope you can understand my point here. The output of C clearly shows what I would like to get. Commented Jun 8, 2020 at 11:36

1 Answer 1

0

This only works when using the first column :

_, indexer, _ = np.intersect1d(B[:,:1], A[:,:1], return_indices=True)

B[indexer]

array([[1, 2, 3, 2],
   [4, 5, 6, 7],
   [7, 8, 9, 4]])

Apparently, the above solution works only if the values are unique (Thanks @AadithyaSaathya.

If we are to use all of A, we could use itertools' product function :

from itertools import product
indexer = [B[0] 
           for A,B 
           in
           product(enumerate(A), enumerate(B[:,:3]))
           if np.all(np.equal(A[-1], B[-1]))]

B[indexer]


array([[1, 2, 3, 2],
       [4, 5, 6, 7],
       [7, 8, 9, 4]])
Sign up to request clarification or add additional context in comments.

2 Comments

import numpy as np A = np.array([[1,2,3],[4,5,6],[7,8,9],[1,4,6]]) B = np.array([[4,5,6,7],[7,8,9,4],[1,4,6,10],[1,2,3,2]]) _, indexer, _ = np.intersect1d(B[:,:1], A[:,:1], return_indices=True) print(B[indexer]) Thanks for your reply. I added a 4th row in A where 1 is repeating in first and fourth row. And now the output is not as expected. The problem now is since it takes only 1 column . For repeating values in a column , the output is not as expected.
let's discard that and try the itertools option then; that should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.