geekygeek
geekygeek

Reputation: 751

How to combine/merge the values of two structured arrays with two keys?

I have two structured arrays with the same keys and data types.

import numpy as np

# Define a dtype with x and y integers    
arr1 = np.empty(6, dtype=[('x', int), ('y', int)])
arr2 = np.empty(8, dtype=[('x', int), ('y', int)])

# Add the data to the structured array
arr1['x'] = np.array([ 32,  32,  32,  32,  32,  39])
arr1['y'] = np.array([449, 451, 452, 453, 454, 463])

arr2['x'] = np.array([ 39,  34,  32,  32,  37,  32 ,23, 12])
arr2['y'] = np.array([463, 393, 453, 452, 261, 449, 1243, 263])

The two structured arrays can have different lengths, as shown. The values occur in x and y pairs.

I would like to combine these two structured arrays, such that

If there is an efficient method for combining more than two structured arrays, such as 3 or 4 of these structured arrays, then a solution would be appreciated.

In the above example, I would like it to merge as follows, where arr3 is the result of merging:

arr3['x'] = np.array([32,32,32,32,32,39,34,37,23,12])
arr3['y'] = np.array([449,451,452,453,454,463,393,261,1243,263]) 

All unique pairs of values between the two structured arrays are in arr3.

I've tried to create some code, but I'm just not sure where to start. Thank you.

Upvotes: 1

Views: 268

Answers (1)

Jérôme Richard
Jérôme Richard

Reputation: 50806

You can simply concatenate the values with np.concatenate and and remove duplicates with np.unique:

arr3 = np.unique(np.concatenate([arr1, arr2]))

Upvotes: 1

Related Questions