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

dmitrey openopt@ukr....
Mon Aug 20 08:54:59 CDT 2007


Steve Schmerler wrote:
> 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]
>   
one more:
x[x!=0].size
D.



More information about the SciPy-user mailing list