Python Forum
Adding a row to a character array
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a row to a character array
#1


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?
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 3,291 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,105 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,673 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Adding markers to Folium map only adding last element. tantony 0 2,094 Oct-16-2019, 03:28 PM
Last Post: tantony
  Replace changing string including uppercase character with lowercase character silfer 11 6,069 Mar-25-2019, 12:54 PM
Last Post: silfer
  Trouble adding LONG strings to an array or CSV output hariskr 1 2,186 Aug-02-2018, 11:29 PM
Last Post: woooee
  SyntaxError: unexpected character after line continuation character Saka 2 18,472 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020