I am trying to write a function to count the occurrences of a number in a list, and the order is ascending according to the number (from 0 to the maximum value in the list), not the occurrences. Here's the function I wrote:
def sort_counts(sample):
result = []
for i in range(max(sample)+1):
result.append(sample.count(i))
return result
For example:
>>> sort_counts([1,2,2,3,3,4,1,1,1,1,2,5])
>>> [0, 5, 3, 2, 1, 1]
I learned that sample.count would work slowly if there are more numbers in the list. Is there a faster/simpler way I can write this function?