Array Printing

Question

What order are array elements printed out by the print command?

Answer

Here's the answer directly from the Numerical Python manual:

The default printing routine provided by the Numeric module prints arrays as follows:
  1. The last axis is always printed left to right
  2. The next-to-last axis is printed top to bottom
The remaining axes are printed top to bottom with increasing numbers of separators.

Thus, rank 1 and 2 arrays will print out on the screen in a form that corresponds to our mental map of their storage in memory:

>>> import Numeric as N
>>> a = N.arange(12)
>>> a
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,]
>>> a = N.reshape(a, (3,4))
>>> a
[[ 0, 1, 2, 3,]
 [ 4, 5, 6, 7,]
 [ 8, 9,10,11,]]

Reference: Ascher, D., et al. (2001): Numerical Python, Livermore, CA: Lawrence Livermore National Laboratory, UCRL-MA-128569, p. 17.

Return to the Tips and Examples index page.

Updated: March 23, 2004 by Johnny Lin <email address>. License.