0

Given two numpy arrays:

a = np.array([[0, 1, 2], [0, 2, 3], [0, 1, 3], [1, 2, 3]])

and

b = np.array([[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 0, 255]])

How do I get the following array from a and b? thanks.

face = np.array([([0, 1, 2], 255, 255, 255),
                 ([0, 2, 3], 255,   0,   0), 
                 ([0, 1, 3],   0, 255,   0), 
                 ([1, 2, 3],   0,   0, 255)])
San Tseng
  • 33
  • 1
  • 4
  • Why would you want to do this? tHe only way is to make an `object` dtype array, but that is essentially a bad python list. So kjust use a `list` – juanpa.arrivillaga Jul 16 '19 at 00:51

3 Answers3

0
In [139]: a=np.array([[0, 1, 2], 
     ...:          [0, 2, 3], 
     ...:          [0, 1, 3], 
     ...:          [1, 2, 3]])                                                                               
In [140]: b=np.array([[255, 255, 255], 
     ...:          [255,   0,   0], 
     ...:          [  0, 255,   0], 
     ...:          [  0,   0, 255]])        

To make a structured array that display like this, use:

In [141]: face = np.zeros(a.shape[0], dtype=[('a',int,3), ('b1',int),('b2',int),('b3',int)]) 

In [142]: face                                                                                               
Out[142]: 
array([([0, 0, 0], 0, 0, 0), ([0, 0, 0], 0, 0, 0), ([0, 0, 0], 0, 0, 0),
       ([0, 0, 0], 0, 0, 0)],
      dtype=[('a', '<i8', (3,)), ('b1', '<i8'), ('b2', '<i8'), ('b3', '<i8')])
In [143]: face['a']=a                                                                                        

The other values have to be set with a list of tuples:

In [145]: face[['b1','b2','b3']] = [tuple(row) for row in b]                                                 
In [146]: face                                                                                               
Out[146]: 
array([([0, 1, 2], 255, 255, 255), ([0, 2, 3], 255,   0,   0),
       ([0, 1, 3],   0, 255,   0), ([1, 2, 3],   0,   0, 255)],
      dtype=[('a', '<i8', (3,)), ('b1', '<i8'), ('b2', '<i8'), ('b3', '<i8')])
In [147]: print(face)                                                                                        
[([0, 1, 2], 255, 255, 255) ([0, 2, 3], 255,   0,   0)
 ([0, 1, 3],   0, 255,   0) ([1, 2, 3],   0,   0, 255)]

Or to make an object dtype array:

In [148]: res = np.zeros((4,4), object)                                                                      
In [151]: res[:,0] = a.tolist()                                                                              
In [152]: res                                                                                                
Out[152]: 
array([[list([0, 1, 2]), 0, 0, 0],
       [list([0, 2, 3]), 0, 0, 0],
       [list([0, 1, 3]), 0, 0, 0],
       [list([1, 2, 3]), 0, 0, 0]], dtype=object)
In [153]: res[:,1:] = b                                                                                      
In [154]: res                                                                                                
Out[154]: 
array([[list([0, 1, 2]), 255, 255, 255],
       [list([0, 2, 3]), 255, 0, 0],
       [list([0, 1, 3]), 0, 255, 0],
       [list([1, 2, 3]), 0, 0, 255]], dtype=object)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

This seems like a job for lists. Here's one way you could do it with a list comprehension:

a = [
    [0, 1, 2],
    [0, 2, 3],
    [0, 1, 3],
    [ 1, 2, 3]
]

b = [
    [255, 255, 255],
    [255,   0,   0],
    [  0, 255,   0],
    [  0,   0, 255]
]

[tuple([a[i]] + b[i]) for i in range(len(a))]
>>> [([0, 1, 2], 255, 255, 255),
     ([0, 2, 3], 255,   0,   0), 
     ([0, 1, 3],   0, 255,   0), 
     ([1, 2, 3],   0,   0, 255)]

If you want to got the numpy route, you're gonna need to find a way to have multiple dtypes within an numpy array. I would suggest take a look at this thread that makes use of numpy structured arrays.

Jay Mody
  • 3,727
  • 1
  • 11
  • 27
0

because of the 'for' its a bit slow.

import numpy as np
a = np.array([[0, 1, 2], [0, 2, 3], [0, 1, 3], [1, 2, 3]])
b = np.array([[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 0, 255]])
s = []
for  i,j in zip(a,b):
    s.append(tuple([tuple(i)]+list(j)))
np.array(s)


Out[90]: 
array([[(0, 1, 2), 255, 255, 255],
       [(0, 2, 3), 255, 0, 0],
       [(0, 1, 3), 0, 255, 0],
       [(1, 2, 3), 0, 0, 255]], dtype=object)
DeepBlue
  • 415
  • 4
  • 9