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?
We can use numpy ndarray tolist() function to convert the array to a list. If the array is multi-dimensional, a nested list is returned. For one-dimensional array, a list with the array elements is returned.
The array object in NumPy is called ndarray . We can create a NumPy ndarray object by using the array() function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With