[SciPy-user] Count

David Warde-Farley dwf@cs.toronto....
Thu Feb 21 16:07:59 CST 2008


One solution is to do a boolean comparison and then call sum() on the  
resulting boolean array. It'll treat the True's as 1's and so you end  
up with the number of occurrences.


In [2]: x = array([2,2,2,2,3,4,5,6,7,8])

In [3]: x == 2
Out[3]: array([ True,  True,  True,  True, False, False, False, False,  
False, False], dtype=bool)

In [4]: sum(x == 2)
Out[4]: 4

There might be other ways, of course.


On 21-Feb-08, at 5:01 PM, R. Padraic Springuel wrote:

> Is there a numpy or scipy command that works on arrays like the count
> property works on lists?
> I.e. if I want to know how many times a certain value occurs in an
> array, is there a single command that will allow me to do that?
> -- 
> R. Padraic Springuel
> Research Assistant
> Department of Physics and Astronomy
> University of Maine
> Bennett 309
> Office Hours: By appointment only
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-user mailing list