3

I have one numpy array =

[1,6,7,9,3,5]

and a second numpy array =

[3,5,8,9,2]

I would like to merge these two arrays together:

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

and then remove the duplicates in the numpy array to get :

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

I would like to keep as much of array one as possible and take out elements of array two, that don't appear in array one, and append these.

I am not sure if it makes more sense to:

  1. merge both arrays and remove duplicates. or
  2. loop through elements of array 2, and if they don't appear in array 1 then concatenate to array 1.

I have tried using various loops but these appear to work mostly for lists, I have also tried using set() but this orders the numpy array, I would like to keep the random order form.

2
  • Do you care about order? Commented Apr 20, 2018 at 22:17
  • Yes, the order of the values in array 1 is important in this case Commented Apr 20, 2018 at 22:18

2 Answers 2

11

To join the two arrays, you can simply use np.concatenate

Removing duplicates while preserving order is a bit tricky, because normally np.unique also sorts, but you can use return_index then sort to get around this:

In [61]: x
Out[61]: array([1, 6, 7, 9, 3, 5])

In [62]: y
Out[62]: array([3, 5, 8, 9, 2])

In [63]: z = np.concatenate((x, y))

In [64]: z
Out[64]: array([1, 6, 7, 9, 3, 5, 3, 5, 8, 9, 2])

In [65]: _, i = np.unique(z, return_index=True)

In [66]: z[np.sort(i)]
Out[66]: array([1, 6, 7, 9, 3, 5, 8, 2])
Sign up to request clarification or add additional context in comments.

6 Comments

This is perfect, thank you ! can I ask you what ""_, """after in[65] does, please?
When you use return_index, you get a sorted list with no duplicates, as well as the preserved indices. Since I don't care about the sorted list, I just use _ as a throwaway variable.
Basically, np.unique() is returning two results and I only care about the second.
Could I ask you one last question, please? I have my arrays with an axis=1. I have specified axis=1 in the concat function, the unique and the sort function. It appears to work as far as the sort function I am getting an error: axis 1 is out of bounds for array of dimension 1, are you familiar with this?
I'm surprised the concat function works when you do that. It throws that error for me.
|
4

Here is slightly different way to do it also:

z = np.concatenate([x, y[~np.isin(y,x)])

2 Comments

Thank you, does this only concat the elements of y that aren't already in x?
Very creative! :)

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.