长度不相等的数组中的numpy数组 [英] Numpy array in array with unequal length
本文介绍了长度不相等的数组中的numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在一个numpy数组中创建一个numpy数组.主数组中的每个数组都应具有不同的长度.主数组应该是内部数组对象的索引号.
I am wondering how to create a numpy array in a numpy array. Each array in the main array should be able to have a different length. The main array should be the index number to the array object inside.
我已经有这种类型的数组的示例,但是我想重新创建类似的数组.
I already have an example of this type of array but I want to re-create something similar.
示例:
test.shape
(200,)
test[0].shape
(5, 10)
test[1].shape
(3, 10)
test[2].shape
(6, 10)
推荐答案
您需要使用dtype=object
创建一个数组,以便numpy知道将每个条目都视为Python对象,而不是将整个嵌套列表都视为一个对象.数组.
You need to create an array with dtype=object
, so that numpy knows to treat each entry as a Python object rather than treating the entire nested list as a single array.
例如:
import numpy as np
x = np.empty((5, 10))
y = np.empty((3, 10))
z = np.empty((6, 10))
test = np.array([x, y, z], dtype=object)
test[0].shape
# [5, 10]
这篇关于长度不相等的数组中的numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文