[Numpy-discussion] filters for rows or columns

Robert Kern robert.kern at gmail.com
Tue Aug 25 19:26:10 EDT 2009


On Tue, Aug 25, 2009 at 16:18, Giuseppe Aprea<giuseppe.aprea at gmail.com> wrote:
> Hi,  I would like to do something like this
>
> a=array([[1,2,3,4],[5,6,7,8],[4,5,6,0]])
> idxList=[]
> for i in range(0,a.shape[1]):
>    if len(nonzero(a[:,i])[0])==1:   #want to extract column indices
> of those columns which only have one non vanishing element
>         idxList.append(i)
>
> I already used where on !D array but I don't know if there is some function or
> some kind of syntax which allow you to evaluate a condition for each
> column(row).

column_mask = ((a != 0).sum(axis=1) == 1)
idxArray = np.nonzero(column_mask)[0]  # if you must

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list