I want to print out various sizes of numpy arrays one after the other in a for loop, but removing the previous print at each step in the loop. A solution often suggested for strings that are less than 1 line long is the following:
import numpy as np
import time
for i in range(5):
a = np.ones(i)
print(a,end = '\r')
time.sleep(0.5)
This works as the array fits on one line and thus the '\r'
argument in the print function works fine, it goes back to the beginning of the line for the next print, so it prints []
then [1.]
then [1. 1. ]
then [1. 1. 1.]
etc.
A problem occurs when the print is larger than one line, and with '\r'
it just goes back to the penultimate line, like so:
for i in range(5):
a = np.ones(shape = (i,i))
print(a,end = '\r')
time.sleep(0.5)
This gives the following:
[[1. 1.]
[[1. 1. 1.]
[1. 1. 1.]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
whereas I would like the arrays to update in size without overlapping on the last line. Essentially, is there a way to replace end = '\r'
with something else so that it goes back to the beginning of the whole print rather than just the beginning of the line? I am doing this in Jupyter notebook with Python 3.8.3