If I initialise a python list
x = [[],[],[]]
print(x)
then it returns
[[], [], []]
but if I do the same with a numpy array
x = np.array([np.array([]),np.array([]),np.array([])])
print(x)
then it only returns
[]
How can I make it return a nested empty list as it does for a normal python list?
It actually does return a nested empty list. For example, try
x = np.array([np.array([]),np.array([]),np.array([])])
>>> array([], shape=(3, 0), dtype=float64)
or
>>> print x.shape
(3, 0)
Don't let the output of print x
fool you. These types of outputs merely reflect the (aesthetic) choices of the implementors of __str__
and __repr__
. To actually see the exact dimension, you need to use things like .shape
.
To return a nested empty list from a numpy array, you can do:
x.tolist()
[[], [], []]
However, even if it prints only []
, the shape is correct:
x.shape
(3, 0)
And you can access any element like a list:
x[0]
array([], dtype=float64)
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