How to initialize Numpy array of list objects

青春壹個敷衍的年華 提交于 2020-01-04 06:00:10

问题


I'm trying to create a numpy array that looks like

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

This array has shape (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?


回答1:


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.



来源:https://stackoverflow.com/questions/57364197/how-to-initialize-numpy-array-of-list-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!