问题
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