+3 votes
in Programming Languages by (75.1k points)
I have a big list of intergers and want to count the number of elements greater than some threshold value. I can use 'for' loop for this task, but is there another approach?


If I have to count how many elements are greater than 5, it should give 10.

1 Answer

+1 vote
by (353k points)
selected by
Best answer

You can convert the list to Numpy array and then use Numpy functions to count the elements greater than a particular value. Check the following example.

>>> a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

>>> a=np.array(a)

>>> np.count_nonzero(a>5)


>>> np.count_nonzero(a<=5)