Why numpy empty arrays don't print

If I initialize a python list

x = [[],[],[]] print(x) 

then he 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 with a regular python list?

+6
source share
3 answers

It actually returns 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 print x pins fool you. These types of outputs simply reflect the (aesthetic) choice of __str__ and __repr__ . To see the exact measurement, you need to use things like .shape .

+8
source

To return a nested empty list from a numpy array, you can do:

 x.tolist() [[], [], []] 

However, even if it prints only [] , the form is correct:

 x.shape (3, 0) 

And you can access any element, such as a list:

 x[0] array([], dtype=float64) 
+3
source

The easiest solution for you is to convert it to a list using tolist

 x = np.array([np.array([]),np.array([]),np.array([])]) print(x) [] print(x.tolist()) [[], [], []] 
+2
source

All Articles