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

Rob Falck robfalck@gmail....
Mon Aug 20 07:04:10 CDT 2007


You could use a list comprehension to return a list containing only the
non-zero elements of the list, and then use the built-in len function to
return its length.

mylist = [3,5,0,9,0,8,9]
num_non_zero = len([x fo x in mylist if x != 0])

Or the count method of a list could be used to get the number of elements
that do equal zero.

num_non_zero = len(mylist) - mylist.count(0)


On 8/20/07, Perez Kawumi <pepe_kawumi@yahoo.co.uk> 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?
>
> ------------------------------
> To help you stay safe and secure online, we've developed the all new *Yahoo!
> Security Centre*<http://us.rd.yahoo.com/mail/uk/taglines/default/security_centre/*http://uk.security.yahoo.com/>
> .
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>


-- 
- Rob Falck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20070820/3919e388/attachment.html 


More information about the SciPy-user mailing list