Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why are empty numpy arrays not printed

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?

like image 366
Sruli Avatar asked Mar 21 '16 14:03

Sruli


2 Answers

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.

like image 89
Ami Tavory Avatar answered Sep 30 '22 10:09

Ami Tavory


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)
like image 41
jrjc Avatar answered Sep 30 '22 11:09

jrjc