Example 1: count similar values in list python
MyList = ["a", "b", "a", "c", "c", "a", "c"]
return my_dict = {i:MyList.count(i) for i in MyList}
{'a': 3, 'c': 3, 'b': 1}
from collections import Counter
return my_dict = dict(Counter(MyList))
{'a': 3, 'c': 3, 'b': 1}
Example 2: count values in array python
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
dict(zip(unique, counts))
Example 3: python how to count all elements in a list
List = ["Elephant", "Snake", "Penguin"]
print(len(List))
Example 4: python how to count items in array
myArray = [1, 2, 3]
len(myArray)
3
Example 5: python count of values in array
import numpy as np
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
num_zeros = (y == 0).sum()
num_ones = (y == 1).sum()