数值数组numpy的对象数组 [英] Numpy object array of numerical arrays
本文介绍了数值数组numpy的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个 DTYPE = np.object
一个数组,其中每个元素是一个数字式的,例如整数或浮点数的数组。例如:
I want to create an array with dtype=np.object
, where each element is an array with a numerical type, e.g int or float. For example:
>>> a = np.array([1,2,3])
>>> b = np.empty(3,dtype=np.object)
>>> b[0] = a
>>> b[1] = a
>>> b[2] = a
创建我想要什么:
Creates what I want:
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
int64
但我想知道是否有不写3行6在同一行(尤其是因为我可能会想连接100阵列)的方法。我试过
but I am wondering whether there isn't a way to write lines 3 to 6 in one line (especially since I might want to concatenate 100 arrays). I tried
>>> b = np.array([a,a,a],dtype=np.object)
但其实这所有的元素转换为np.object:
but this actually converts all the elements to np.object:
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
object
没有人有任何想法如何避免这种情况?
Does anyone have any ideas how to avoid this?
推荐答案
这不完全pretty,但是......
It's not exactly pretty, but...
import numpy as np
a = np.array([1,2,3])
b = np.array([None, a, a, a])[1:]
print b.dtype, b[0].dtype, b[1].dtype
# object int32 int32
这篇关于数值数组numpy的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文