0

I have two 2d-arrays (but rows doesn't have same length) created with numpy:

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

b = [[1,2,30,40,50],
     [6,7,80,90,100]]

I would like to combine this two arrays into a new array, keeping the repeated values and adding the new ones "row-wise":

#desired output

c = [[1,2,3,4,5,30,40,50],
     [6,7,8,9,80,90,100]]

I tried many approaches, including np.apply_along_axis with np.unique or simply looping each row and appending to a list and then creating an array form that list. The closest result i got was an array of arrays, like so:

array(array([1,2,3,4,5,30,40,50]), array([6,7,80,90,100]))

The above result isn't helpful, i need a numpy array. Any help will be appreciated.

4
  • a has two elements, one with length 5 and one with length 4. Is it intentional? Commented Sep 23, 2020 at 21:23
  • Yes, they won't share the same dimensions. Commented Sep 23, 2020 at 21:28
  • is there any duplicate in each sub-array of a and b ? Commented Sep 23, 2020 at 22:28
  • Can't ensure that, since i will apply this a machine learning algorithm with any dataset. Commented Sep 24, 2020 at 21:12

1 Answer 1

1

Let's use union:

[np.union1d(x,y) for x,y in zip(a,b)]

Output:

[array([ 1,  2,  3,  4,  5, 30, 40, 50]),
 array([  6,   7,   8,   9,  80,  90, 100])]

If you really need list of lists:

[np.union1d(x,y).tolist() for x,y in zip(a,b)]

Output:

[[1, 2, 3, 4, 5, 30, 40, 50], [6, 7, 8, 9, 80, 90, 100]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this will do the job!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.