0

I have a 2D array of 4x4 matrix. I am printing the array in formatted form. Now I want to add a sleep time of say 1 sec and then for two of the elements in array (I know the index), want to overwrite those two elements with the values in the same print.

I have tried adding end = "\r" and then updating the array elements, But this is not helping. It's still printing the new array underneath the previous print.

print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in duplicate_array]), end = "\r")
time.sleep(1)
duplicate_array[0] = 0
duplicate_array[1] = 1
print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in duplicate_array]))

Expected output -

a  a  2  3  
4  5  6  a  
8  9  10 11  
12 13 14 15  

I want to display the above array first and then after 1 sec lapse, I want to just overwrite the elements where the value is 'a' with respective indexes. I this case 0 and 1.

After 1 sec array will become -

0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

I don't want to overwrite the whole array, just the elements at 0 and 1. So that user sees just those elements changing and not the whole array.

Actual Output with the code I have -

a   a   2   3
4   5   6   7
8   9   10  11
a   a   2   34  15
4   5   6   7
8   9   10  11
12  13  14  15

Christina
  • 117
  • 1
  • 8
  • `\r` will let you overwrite a single line, but to re-print multiple lines you'll need something more sophisticated like the [curses](https://docs.python.org/3/howto/curses.html) library. – John Kugelman Apr 21 '19 at 14:11
  • also see [this](https://stackoverflow.com/a/6840469/2572080) answer – Pythoscorpion Apr 21 '19 at 18:32

1 Answers1

0

if you don't want to use third party libraries, so this code is best practice for you. i got expected result on windows

import os, time


array = [['a','a' ,'2', '3'], ['4',  '5',  '6',  'a'], ['12', '13', '14', '15']]

print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in array]), end = "\r")

time.sleep(2)

array[0][0] = '0'
array[0][1] = '1'
array[1][3] = '7'

os.system('cls')

print('\n'.join(['\t'.join([str(cell) for cell in row]) for row in array]), end = "\r")

input()
Pythoscorpion
  • 166
  • 1
  • 10