Devon  Oliver
Devon Oliver

Reputation: 345

How do I get a count of values in an array, in a range or above a threshold value?

I want to get a count of values greater than 350 and a second count of values greater than 510. I just need a single number for each count. I have seen some ways of getting counts, but they didn't appear to work for my purpose. Thanks for any help.

print(simfourty48)
[ array([  99.06,  180.15,  234.21,  369.36,  171.14,  162.13,   54.01,
        324.31,  171.14,  108.07,  225.2 ,  243.22,  306.29,  144.11,
        450.45,  693.72,  225.2 ,  486.49,  810.85,  243.22,  279.26,
        135.1 ,  261.24,  405.4 ,  126.09,  261.24,  162.13,  234.21,   45.  ])]

Upvotes: 1

Views: 185

Answers (2)

ranlot
ranlot

Reputation: 656

sum(filter(lambda x: x > 350, simfourty48))

Upvotes: 0

mgilson
mgilson

Reputation: 309929

Have you tried numpy.count_nonzero?

np.count_nonzero(simfourty48 > 350)
np.count_nonzero(simfourty48 > 510)

Actually, looking at simfourty48, it looks like you have a list whose only element is an np.ndarray. In that case it would be:

np.count_nonzero(simfourty48[0] > 350)

But I'd recommend rethinking the list with 1 array datastructure if possible. That seems like it will just end up making your life more difficult.

Upvotes: 2

Related Questions