Home > Software engineering >  How to combine/merge the values of two structured arrays with two keys?
How to combine/merge the values of two structured arrays with two keys?

Time:04-15

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

  • Values that are shared between them (same x and y value combination) are not duplicated and will occur only once
  • Values that are not shared between them (different x and y value combination) will be included in the merged structure array
  • An order or pattern is not needed for how values are appended. As long as the resulting contains all x and y pairs that exist between the two, and are unique.

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.

CodePudding user response:

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

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