0

How to combine the two 2D arrays as shown?

sample_list = [np.array([1, 2, 3]), np.array([4, 5, 6)])]
sample_objects = [np.array([object_1, object_2, object_3]), np.array([object_4, object_5, object_6])]

result = [np.array([(1, object_1), (2, object_2), (3, object_3)]), np.array([(4, object_4), (5, object_5), (6, object_)])
1
  • 1
    Why do you want to mix integers and object_n? Wouldn't it simpler to stick with lists? Commented Apr 5, 2020 at 23:42

2 Answers 2

1

Try the following using zip:

result = [np.array(list(zip(sample_list[i], sample_objects[i]))) for i in range(len(sample_list))]
Sign up to request clarification or add additional context in comments.

Comments

0

A pure list version of your problem:

In [121]: sample_list = [[1, 2, 3], [4, 5, 6]] 
     ...: sample_objects = [["object_1", "object_2", "object_3"], ["object_4", "object_5", "object_6"]]                                                                             
In [122]: sample_list                                                                          
Out[122]: [[1, 2, 3], [4, 5, 6]]
In [123]: sample_objects                                                                       
Out[123]: [['object_1', 'object_2', 'object_3'], ['object_4', 'object_5', 'object_6']]
In [124]: [list(zip(a,b)) for a,b in zip(sample_list, sample_objects)]                         
Out[124]: 
[[(1, 'object_1'), (2, 'object_2'), (3, 'object_3')],
 [(4, 'object_4'), (5, 'object_5'), (6, 'object_6')]]

Wrapping things in np.array adds little, if anything, to the problem. Note I had to substitute strings for your objects.

sample_objects, if it really contains 'objects' while be object dtype:

sample_objects = [np.array(["object_1", "object_2", "object_3"],object), 
    np.array(["object_4", "object_5", "object_6"],object)]  

====

Define a sample class

class MyObj:
    def __init__(self,n):
        self.n = n
    def __repr__(self):
        return f'object_{self.n}'

and your arrays:

In [141]: sample_list = [np.array([1, 2, 3]), np.array([4, 5, 6])] 
     ...: sample_objects = [np.array([MyObj(n) for n in [1,2,3]]), np.array([MyObj(n) for n in 
     ...: [4,5,6]])]                                                                                      
In [142]: sample_list                                                                          
Out[142]: [array([1, 2, 3]), array([4, 5, 6])]
In [143]: sample_objects                                                                       
Out[143]: 
[array([object_1, object_2, object_3], dtype=object),
 array([object_4, object_5, object_6], dtype=object)]

And the list zip way of combining them:

In [153]: [np.array(list(zip(a,b))) for a,b in zip(sample_list, sample_objects)]               
Out[153]: 
[array([[1, object_1],
        [2, object_2],
        [3, object_3]], dtype=object), array([[4, object_4],
        [5, object_5],
        [6, object_6]], dtype=object)]

and a variant using np.stack:

In [154]: [np.stack((a,b), axis=1) for a,b in zip(sample_list, sample_objects)]                
Out[154]: 
[array([[1, object_1],
        [2, object_2],
        [3, object_3]], dtype=object), array([[4, object_4],
        [5, object_5],
        [6, object_6]], dtype=object)]

hstack does not interleave them:

array([1, 2, 3, object_1, object_2, object_3], dtype=object)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.