import collections a = [1,1,1,1,2,2,2,2,3,3,4,5,5] counter=collections.Counter(a) print(counter) # Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1}) print(counter.values()) # [4, 4, 2, 1, 2] print(counter.keys()) # [1, 2, 3, 4, 5] print(counter.most_common(3)) # [(1, 4), (2, 4), (3, 2)]
freq = {} for item in my_list: if (item in freq): freq[item] += 1 else: freq[item] = 1
Format: string.count(sub, start= 0,end=len(string)) string = "Add Grepper Answer" print(string.count('e') >>> 3
# string in which occurrence will be checked string = "geeks for geeks" # counts the number of times substring occurs in # the given string and returns an integer print(string.count("geeks"))
it counts the number of elements in the list or in the string(words)