Python Forum
How do you change specific elements in a char array of string? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How do you change specific elements in a char array of string? (/thread-5967.html)



How do you change specific elements in a char array of string? - JoeB - Oct-30-2017



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?


RE: How do you change specific elements in a char array of string? - Mekire - Oct-31-2017

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.



RE: How do you change specific elements in a char array of string? - JoeB - Oct-31-2017

(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')



RE: How do you change specific elements in a char array of string? - cryomick - Oct-31-2017

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?


RE: How do you change specific elements in a char array of string? - JoeB - Oct-31-2017

(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.