To count the elements in a list, you can use collections.Counter, but what if only some of the elements have to be counted?
I\'ve set up this example (please note: numpy
If this is about large numpy arrays you'd better take advantage of vectorized numpy operations.
%%time
np.unique(numbers[numbers <= 10], return_counts=True)
Output:
Wall time: 31.2 ms
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
array([10055, 10090, 9941, 10002, 9994, 9989, 10070, 9859, 10038,
10028, 9965], dtype=int64))
For comparison, my own timing of your code gave slighly higher times than yours.