Posts: 23
Threads: 13
Joined: Oct 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?
Posts: 591
Threads: 26
Joined: Sep 2016
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.
>>> a = np.chararray((3,3))
>>> a[:] = "a" #set whole array to element
>>> a
chararray([['a', 'a', 'a'],
['a', 'a', 'a'],
['a', 'a', 'a']],
dtype='|S1')
>>> a[0,:] = "b" # set whole row to element
>>> a
chararray([['b', 'b', 'b'],
['a', 'a', 'a'],
['a', 'a', 'a']],
dtype='|S1')
>>> a[:,1] = "c" # set whole column to element
>>> a
chararray([['b', 'c', 'b'],
['a', 'c', 'a'],
['a', 'c', 'a']],
dtype='|S1')
>>> a[1,2] = "d" # set single element
>>> a
chararray([['b', 'c', 'b'],
['a', 'c', 'd'],
['a', 'c', 'a']],
dtype='|S1')
>>> a[2][2] = "e" # set single element
>>> a
chararray([['b', 'c', 'b'],
['a', 'c', 'd'],
['a', 'c', 'e']],
dtype='|S1')
>>>
Posts: 23
Threads: 13
Joined: Oct 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.
>>> a = np.chararray((3,3))
>>> a[:] = "a" #set whole array to element
>>> a
chararray([['a', 'a', 'a'],
['a', 'a', 'a'],
['a', 'a', 'a']],
dtype='|S1')
>>> a[0,:] = "b" # set whole row to element
>>> a
chararray([['b', 'b', 'b'],
['a', 'a', 'a'],
['a', 'a', 'a']],
dtype='|S1')
>>> a[:,1] = "c" # set whole column to element
>>> a
chararray([['b', 'c', 'b'],
['a', 'c', 'a'],
['a', 'c', 'a']],
dtype='|S1')
>>> a[1,2] = "d" # set single element
>>> a
chararray([['b', 'c', 'b'],
['a', 'c', 'd'],
['a', 'c', 'a']],
dtype='|S1')
>>> a[2][2] = "e" # set single element
>>> a
chararray([['b', 'c', 'b'],
['a', 'c', 'd'],
['a', 'c', 'e']],
dtype='|S1')
>>>
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')
Posts: 12
Threads: 1
Joined: Oct 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?
Posts: 23
Threads: 13
Joined: Oct 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.
|