I am trying to delete all rows in which there is one or less non-zero elements, in multiple 2D arrays contained within the list 'a'.
This method works when I run it outside the 'i' loop, but does not as a whole. I know that I cannot delete rows over which I am iterating, but I believe that I am not doing so in this case, because I am only deleting rows in arrays contained in a, not the arrays themselves.
for i in range(len(a)):
del_idx=[]
for j in range(len(a[i])):
nonzero=np.nonzero(a[i][j])
nonzero_len=len(nonzero[0]) #because np.nonzero outputs a tuple
if nonzero_len<=1:
del_idx.append(j)
else:
continue
np.delete(a[i],(del_idx),axis=0)
Anyone know what's going on here? If this really does not work, how can I delete these elements without using a loop? This is Python 2.7
Thank you!