Writting in sub-ndarray of a ndarray in the most pythonian way. Python 2

前端 未结 2 1628
有刺的猬
有刺的猬 2021-01-25 20:28

I have a ndarray like this one:

number_of_rows = 3
number_of_columns = 3
a = np.arange(number_of_rows*number_of_columns).reshape(number_of_rows,number_of_columns         


        
2条回答
  •  醉梦人生
    2021-01-25 20:57

    For the continuous rows and columns case, you can use basic slicing like this:

    In [634]: a
    Out[634]: 
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    
    In [635]: b = np.asarray([[100, 101],[102, 103]])
    
    In [636]: a[:rows_to_keep[1]+1, columns_to_keep[0]:] = b
    
    In [637]: a
    Out[637]: 
    array([[  0, 100, 101],
           [  3, 102, 103],
           [  6,   7,   8]])
    

提交回复
热议问题