Python Forum
Adding a row to a character array - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Adding a row to a character array (/thread-6461.html)



Adding a row to a character array - JoeB - Nov-23-2017



m = np.chararray((10, 10))
m[:] = "*"
m(5, 4), m(5, 6) = "O", "O"
So I have the following character array:

Output:
m = [['*' '*' '*' '*' '*' '*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*' '*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*' '*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*' '*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*' '*' '*' '*' '*' '*'] ['*' '*' '*' '*' 'O' '*' 'O' '*' '*' '*'] ['*' '*' '*' '*' '*' '*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*' '*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*' '*' '*' '*' '*' '*'] ['*' '*' '*' '*' '*' '*' '*' '*' '*' '*']]
I want to insert a row at the beginning and the end. Specifically, I want the following character array:

Output:
m = [['X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' 'O' '*' 'O' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' '*' '*' '*' '*' '*' '*' '*' '*' '*' '*' 'X'] ['X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X' 'X']]
Adding the values of 'X' to the end of each row is easily done with an append. However, I cannot find any way of inserting an entire row to the beginning and the end of the array. Inserting an element to the beginning of each row is also proving difficult. Any ideas?


RE: Adding a row to a character array - Larz60+ - Nov-23-2017

I thought this might work:
>>> np.concatenate([[b'*', b'*', b'*', b'*', b'O', b'*', b'O', b'*', b'*', b'*'], m, [b'*', b'*', b'*', b'*', b'O', b'*', b'O', b'*', b'*', b'*']])
but it raises the error
Error:
Traceback (most recent call last):   File "<stdin>", line 1, in <module> ValueError: all the input arrays must have same number of dimensions
It probably is something very similar