I was trying to do some simple manipulation of lists and numpy arrays and got stuck in some easy thing:
a=np.arange(12)
a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
a=np.reshape(a,(3,4))
a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
b=np.arange(12,24)
b
array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
b=np.reshape(3,4)
list1 = [(a,'a'),(b,'b')]
data = [(i, j) for i,j in list1]
When I tried to do:
data[0][0]=np.delete(data[0][0], np.s_[-1::],0)
I got the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
But if I do:
cop=np.delete(data[0][0], np.s_[-1::],0)
cop
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
It works perfectly fine.
But I also can’t do:
data[0][0]=np.copy(cop)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
But if I check the types, both are actually arrays:
type(cop)
<type 'numpy.ndarray'>
type(data[0][0])
<type 'numpy.ndarray'>
I couldn’t find the mistake for quite a few hours.
Then I realized that data is actually a tuple.
So this is what solves the problem:
And then I can replace elements like data[0][0]