Python Forum
How do you change specific elements in a char array of string?
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you change specific elements in a char array of string?
#1


Consider the following char array:

a = chararray((3, 3))
print a
which is displayed as

Output:
chararray([['', '', ''], ['', '', ''], ['', '', '']], dtype='|S1')
I want to change a specific element. So far I have only figured out how to edit entire rows or the entire array. For example:

a[:] = 'b'
print a

a[1:2] = 'c'
print a

#a[1:] does nothing etc etc
Output:
Out[10]: chararray([['b', 'b', 'b'], ['b', 'b', 'b'], ['b', 'b', 'b']], dtype='|S1') Out[12]: chararray([['b', 'b', 'b'], ['c', 'c', 'c'], ['b', 'b', 'b']], dtype='|S1')
I have tried things such as a[1][2]='K' to no avail. I would think that this would edit the element corresponding to the row in index 1, and the column in index 2, but it doesn't.

Some help please?
Reply
#2
Firstly, you need to make it clear you are using numpy.  chararray is not a builtin type.

Secondly,
Quote:I would think that this would edit the element corresponding to the row in index 1, and the column in index 2, but it doesn't.
It absolutely DOES work this way.
Reply
#3
(Oct-31-2017, 03:30 AM)Mekire Wrote: Firstly, you need to make it clear you are using numpy.  chararray is not a builtin type.

Secondly,
Quote:I would think that this would edit the element corresponding to the row in index 1, and the column in index 2, but it doesn't.
It absolutely DOES work this way.

OK. So why isn't the following working:

Output:
In [33]: b = numpy.chararray((4, 4)) In [34]: b Out[34]: chararray([['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']], dtype='|S1') In [35]: b[1][2] = 'a' In [36]: b Out[36]: chararray([['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ['', '', '', '']], dtype='|S1')
Reply
#4
What IDE are you using? I tried this in IDLE and got the following result:

>>>a = np.chararray((3,3))
>>> a
chararray([['', '\x01', ''],
       ['\x01', '', ''],
       ['\x01', '\x01', '\x01']], 
      dtype='|S1')
>>> a[0][0]='b'
>>> a
chararray([['b', '\x01', ''],
       ['\x01', '', ''],
       ['\x01', '\x01', '\x01']], 
      dtype='|S1')
>>> a[1][2]='a'
>>> a
chararray([['b', '\x01', ''],
       ['\x01', '', 'a'],
       ['\x01', '\x01', '\x01']], 
      dtype='|S1')
>>> a[0][1]='c'
>>> a
chararray([['b', 'c', ''],
       ['\x01', '', 'a'],
       ['\x01', '\x01', '\x01']], 
      dtype='|S1')
Isn't this what you were expecting?
Reply
#5
(Oct-31-2017, 08:37 AM)cryomick Wrote: What IDE are you using? I tried this in IDLE and got the following result:

>>>a = np.chararray((3,3))
>>> a
chararray([['', '\x01', ''],
       ['\x01', '', ''],
       ['\x01', '\x01', '\x01']], 
      dtype='|S1')
>>> a[0][0]='b'
>>> a
chararray([['b', '\x01', ''],
       ['\x01', '', ''],
       ['\x01', '\x01', '\x01']], 
      dtype='|S1')
>>> a[1][2]='a'
>>> a
chararray([['b', '\x01', ''],
       ['\x01', '', 'a'],
       ['\x01', '\x01', '\x01']], 
      dtype='|S1')
>>> a[0][1]='c'
>>> a
chararray([['b', 'c', ''],
       ['\x01', '', 'a'],
       ['\x01', '\x01', '\x01']], 
      dtype='|S1')
Isn't this what you were expecting?
Interesting. I'm just using pylab on terminal.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Join each list elements with string in other DF handy88 0 1,537 Feb-09-2021, 07:00 PM
Last Post: handy88
  How to search for specific string in Pandas dataframe Coding_Jam 1 2,432 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  Formula with elements of list - If-condition regarding the lists elements lewielewis 2 2,736 May-08-2020, 01:41 PM
Last Post: nnk
  define certain array elements+display with digits lukezo 0 1,253 Apr-10-2020, 05:03 PM
Last Post: lukezo
  How to prepare a NumPy array which include float type array elements subhash 0 1,906 Mar-02-2020, 06:46 AM
Last Post: subhash
  I am trying to change the value of an element in a record array ingu 1 2,168 Jan-14-2020, 01:30 PM
Last Post: perfringo
  numpy.where array search for string in just one coordinate adetheheat 1 2,277 Jan-09-2020, 07:09 PM
Last Post: paul18fr
  Extracting specific columns in an array uthongam 1 2,160 Dec-07-2018, 06:06 PM
Last Post: ichabod801
  Checking the elements of a matrix with an elements of a list juniorcoder 11 5,868 Sep-17-2018, 03:02 PM
Last Post: gruntfutuk
  How can i convert a string to an array with elements type float 64 zoya2385 3 6,124 May-11-2017, 03:57 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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