简体   繁体   English

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

[英]How to combine list with integer in an array of tuples?

Given two numpy arrays: 给定两个numpy数组:

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? 如何从a和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)])
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: 或制作一个对象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)

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. 如果要获取numpy路由,则需要找到一种在numpy数组中具有多个dtype的方法。 I would suggest take a look at this thread that makes use of numpy structured arrays . 我建议您看一下使用numpy结构化数组的线程

because of the 'for' its a bit slow. 因为“ 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM