Dynamic array in Python
<p>IÂ´m trying to delete an specific row and column from a square list "m"  iteratively. At the beginning, I used the square list like a square matrix "m" and I tried using the comand "delete" from numpy as follows:</p>

<pre><code>from numpy import*
import numpy as np
m=array([[1,2,3],[4,5,6],[7,8,9]])
#deleting row and column "0"
#x is the new matrix without the row and column "0"
x=np.delete((np.delete(m,0,0)),0,1)
print x
x=[[5,6],[8,9]]
</code></pre>

<p>The problem with the command "delete" itÂ´s IÂ´m not sure about this command can be used in a iteratively loop. Specifically, I want to known how I can delete and specific row and column taking from a "y" list:</p>

<pre><code>m=([[1,2,3],[4,5,6],[7,8,9]])
y=[[1],[0],[1],[0]]
</code></pre>

<p>Note: If the number "1" appears in the list "y", in the list "m" delete the respective row and column.</p>

<p>If number "1" appears in the list "y", delete the respective row and column in the list "m". For example,  in this case the number "1" appears in the list "y" in the position "0", we need to delete the first row and the first column in the list "m". This is the desired list "m" for the first apperance of the number "1" in the list "y":
    m=[[5,6],[8,9]]</p>

<p>Note: The size of the list "m" changed, now is 2x2. This is my first question, how I can use a dinamyc list in Python?. How can a specify the new dimension?. </p>

<p>Because the number "1" appears again in the list "y", we need to delete the respective row and column in the new list "m", in this case the desired list "m" is:
    m=[[5]]</p>

<p>I tried in a lot of ways, I obtained this tip from this forum (another way using comprehension list): </p>

<pre><code>#row to delete
roww=0
#column to delete
column=0
m=([[1,2,3],[4,5,6],[7,8,9]])
a=[j[:column]+j[column+1:] for i,j in enumerate(m) if i!=roww]
print a
</code></pre>

<p>Note: I donÂ´t know how use the dynamic arrays in python, because I need some help. Some people thinks is a homework,but is a lie, I try to learn Python because is more friendly. I used to use Fortran, but with Python I hate that. Thanks.</p>

<p>IÂ´m trying to incorporate an array which specify which rows and columns must be deleted. This is my original idea:</p>

<pre><code>from numpy import*
import numpy as np
mat=array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
x=[[1],[0],[1],[0]]
for i in x:
   if i==1:
     row=0
     col=0
     mat=np.delete(np.delete(mat,row,0),col,1)
print mat
</code></pre>

<p>When one appears in the first row(row 0) in "x", this is the desired matrix:</p>

<pre><code>mat=array([[6,7,8],[10,11,12],[14,15,16]]
</code></pre>

<p>When one appears in the third row(row 2) in "x", this is the desired matrix:</p>

<pre><code>mat=array([[6,8], [14,16]])
</code></pre>

<p>Note: x gives the index to delete in the original matrix "mat".</p>

<p>Thanks again.</p>

<p>It seems to me that applying <code>np.delete</code> repeatedly to the same matrix does the job: </p>

<pre><code>from __future__ import print_function
import numpy as np

mat = np.array([[1,2,3],[4,5,6],[7,8,9]])
print( "initial mat:\n", mat )

rmlist = [ (2,0), (1,1) ]      # a list of (row,col) to be removed
# rmlist = [ [2,0], [1,1] ]    # this also works

for (row, col) in rmlist:
    print( "removing row", row, "and column", col )
    mat = np.delete( np.delete( mat, row, 0 ), col, 1 )
    print( mat )
</code></pre>

<p>Result (is this not what you expect...?):</p>

<pre><code>initial mat:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
removing row 2 and column 0
[[2 3]
 [5 6]]
removing row 1 and column 1
[[2]]
</code></pre>

<hr>

<p><strong>EDIT</strong></p>

<p>If you need to retain specific rows and columns of the original matrix, you can achieve it by selecting rows and columns directly such that</p>

<pre><code>print( "initial mat = \n", mat )
print( "sliced mat = \n", mat[ :, [1, 3] ] )
print( "further sliced mat = \n", mat[ :, [1, 3] ][ [1,3] ] )
</code></pre>

<p>where <code>[1,3]</code> means that 1st and 3rd rows (or columns) should be retained. Using the 4x4 matrix in the Question, the result becomes</p>

<pre><code>initial mat = 
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
sliced mat = 
 [[ 2  4]
 [ 6  8]
 [10 12]
 [14 16]]
further sliced mat = 
 [[ 6  8]
 [14 16]]
</code></pre>

<p>If necessary, you can also create the above selection list (like [1,3]) from x = [1,0,1,0] as follows:</p>

<pre><code>x = [ 1, 0, 1, 0 ]
s = []
for i in range( len(x) ):
    if x[i] == 0: s.append( i )

print( "s = ", s )  # s = [1,3]

print( "further sliced mat (again) = \n", mat[ :, s ][ s ] )
</code></pre>

python arrays dynamic