[Numpy-discussion] ndarray.count() ?

Martin Spacek scipy at mspacek.mm.st
Thu Sep 7 15:17:58 CDT 2006

What's the most straightforward way to count, say, the number of 1s or 
Trues in the array? Or the number of any integer?

I was surprised to discover recently that there isn't a count() method 
as there is for Python lists. Sorry if this has been discussed already, 
but I'm wondering if there's a reason for its absence.

I came across a thread in March:


that talked a bit about this in terms of speed, but what about just the 
convenience of having a count() method?

Looks like masked arrays have a count method, don't know much about them 

Also, I understand the inaccuracies when converting between binary and 
decimal floating point representations, and therefore making counting of 
a specific float value in an array somewhat undefined, yet it seems to 
work in Python lists:

 >>> 1.1
 >>> a=[1.1, 1.1, 1.2]
 >>> a
[1.1000000000000001, 1.1000000000000001, 1.2]
 >>> a.count(1.1)
 >>> a.count(1.1000000000000001)
 >>> a.count(1.2)



More information about the Numpy-discussion mailing list