简体   繁体   English

如何初始化列表对象的Numpy数组

[英]How to initialize Numpy array of list objects

I'm trying to create a numpy array that looks like 我正在尝试创建一个看起来像的numpy数组

array([[list([]), list([])],
       [list([]), list([])],
       [list([]), list([])]], dtype=object)

This array has shape (3,2) . 该数组的形状为(3,2) However, whenever I do 但是,每当我这样做

np.array([[list(), list()], [list(), list()], [list(), list()]])

I end up getting 我最终得到

array([], shape=(3, 2, 0), dtype=float64)

How do I solve this? 我该如何解决?

You could use the following: 您可以使用以下内容:

np.frompyfunc(list, 0, 1)(np.empty((3,2), dtype=object))  

We first turn list into a ufunc that takes no arguments and returns a single empty list, then apply it to an empty 3x2 array of object type. 我们首先将list变成一个不带参数的ufunc ,并返回一个空列表,然后将其应用于对象类型的3x2空数组。

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

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