如何在元组数组中将列表与整数合并?

时间:2019-07-16 00:50:59

标签: python numpy

给出两个numpy数组:

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]])

如何从a和b获取以下数组?谢谢。

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)])

3 个答案:

答案 0 :(得分: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]])        

要使结构化数组显示如下,请使用:

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                                                                                        

其他值必须通过元组列表设置:

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)]

或创建对象dtype数组:

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)

答案 1 :(得分:0)

这似乎是一份清单工作。您可以通过一种列表理解方法来做到这一点:

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)]

如果要获取numpy路由,则需要找到一种在numpy数组中具有多个dtypes的方法。我建议看看这个thread that makes use of numpy structured arrays

答案 2 :(得分:0)

因为'for'有点慢。

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)