Python Forum

Full Version: Adding a row to a character array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.


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?
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