[SciPy-user] finding number of elements in a vector

Steve Schmerler elcorto@gmx....
Mon Aug 20 08:39:14 CDT 2007


Perez Kawumi wrote:
> Hi,
> say i have a vector [3,5,0,9,0,8,9]
> is there any method in python i can use to find out how many non-zero 
> vector elements are in this list?
> 

from numpy import array

x = array([3,5,0,9,0,8,9])

x[x!=0].shape[0]
or
len(x[x!=0])
or
x.nonzero()[0].shape[0]

-- 
cheers,
steve

Random number generation is the art of producing pure gibberish as 
quickly as possible.


More information about the SciPy-user mailing list