我想用 dtype=np.object
创建一个数组,其中每个元素都是一个数字类型的数组,例如 int 或 float。例如:
>>> a = np.array([1,2,3])
>>> b = np.empty(3,dtype=np.object)
>>> b[0] = a
>>> b[1] = a
>>> b[2] = a
创建我想要的:
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
int64
但我想知道是否没有办法将第 3 行到第 6 行写在一行中(特别是因为我可能想连接 100 个数组)。我试过了
>>> b = np.array([a,a,a],dtype=np.object)
但这实际上将所有元素转换为 np.object:
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
object
有没有人知道如何避免这种情况?
最佳答案
它不是很漂亮,但是......
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
关于python - 数值数组的 Numpy 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3273040/