あなたはほとんどそこにいます。
必要なものは次のとおりです。
u[row_col_index[0], row_col_index[1]] = u_pres
説明:
あなたは Python の初心者だと言っているので (私もそうです!)、これを教えてあげようと思いました。あなたがしたようにモジュールをロードすることは非Pythonicと見なされます:
#BAD
from numpy import *
#GOOD
from numpy import array #or whatever it is you need
#GOOD
import numpy as np #if you need lots of things, this is better
説明:
In [18]: u = np.zeros(10)
In [19]: u
Out[19]: array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
#1D assignment
In [20]: u[0] = 1
In [21]: u[1] = 10
In [22]: u[-1] = 9 #last element
In [23]: u[-2] = np.pi #second last element
In [24]: u
Out[24]:
array([ 1. , 10. , 0. , 0. ,
0. , 0. , 0. , 0. ,
3.14159265, 9. ])
In [25]: u.shape
Out[25]: (10,)
In [27]: u[9] #calling
Out[27]: 9.0
#2D case
In [28]: y = np.zeros((4,2))
In [29]: y
Out[29]:
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
In [30]: y[1] = 10 #this will assign all the second row to be 10
In [31]: y
Out[31]:
array([[ 0., 0.],
[ 10., 10.],
[ 0., 0.],
[ 0., 0.]])
In [32]: y[0,1] = 9 #now this is 2D assignment, we use 2 indices!
In [33]: y[3] = np.pi #all 4th row, similar to y[3,:], ':' means all
In [34]: y[2,1] #3rd row, 2nd column
Out[34]: 0.0
In [36]: y[2,1] = 7
In [37]:
In [37]: y
Out[37]:
array([[ 0. , 9. ],
[ 10. , 10. ],
[ 0. , 7. ],
[ 3.14159265, 3.14159265]])
あなたの場合、行に使用される ( ) の最初の配列とrow_col_index
、row_col_index[0]
列に使用される2番目の配列 ( row_col_index[1]
) がありました。
最後に、ipythonを使用していない場合は、使用することをお勧めします。学習プロセスやその他の多くの点で役立ちます。
これが役立つことを願っています。