[Numpy-discussion] ndarray.count() ?

Martin Spacek scipy at mspacek.mm.st
Thu Sep 7 16:17:58 EDT 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:

http://aspn.activestate.com/ASPN/Mail/Message/numpy-discussion/3066460

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 
though.

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
1.1000000000000001
 >>> a=[1.1, 1.1, 1.2]
 >>> a
[1.1000000000000001, 1.1000000000000001, 1.2]
 >>> a.count(1.1)
2
 >>> a.count(1.1000000000000001)
2
 >>> a.count(1.2)
1

Comments?

Martin




More information about the NumPy-Discussion mailing list