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

Steve Schmerler elcorto at gmx.net
Mon Aug 20 09:39:14 EDT 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