0

Suppose I have 2 numpy arrays:

import numpy as np
a = np.array([1, 2,      3,      4     ], dtype='float64')
b = np.array([1, 2.0034, 4,      5.4322], dtype='float64')
c = np.array([1, 2,      3.1234, 5.321 ], dtype='float64')
print('a is : ', a)
print('b is : ', b)
print('c is : ', c)

This will print as:

a is [1. 2. 3. 4.]
b is [1.     2.0034 4.     5.4322]
c is [1.     2.     3.1234 5.321 ]
# How nicely the bottom two arrays are aligned! Because they have equal precessions.

But I want to print them all as aligned. Like below:

a is [1.     2.     3.     4.]
b is [1.     2.0034 4.     5.4322]
c is [1.     2.     3.1234 5.321 ]
Rahat Zaman
  • 1,322
  • 14
  • 32
  • In addition to that other question providing a good approach, the problem here is that three separate print statements don't share any information, which causes all three to print differently. You'd have to write your own print routine to maintain state across all of them, or use one that would have the appropriate formatting included in the string or other parameters. – Grismar Mar 26 '19 at 01:49
  • I agree with Grismar, in case you don't mind trailing zeros, then you can try, as in [this answer](https://stackoverflow.com/a/8885688/3293508): `np.set_printoptions(formatter={'float': '{:.4f}'.format})` – Nguyen Cong Mar 26 '19 at 01:55
  • `np.array2string(np.vstack((a,b,c))).splitlines() ` gives you 3 aligned lines, which you could edit and use in the final display. What I've done is join the 3 arrays into one 2d array, and format that. – hpaulj Mar 26 '19 at 02:09

0 Answers0