Nayuki
0
Q:

freq count in python

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)]
2
freq = {} 
    for item in my_list: 
        if (item in freq): 
            freq[item] += 1
        else: 
            freq[item] = 1
3
Format: string.count(sub, start= 0,end=len(string))
string =  "Add Grepper Answer"
print(string.count('e')
>>> 3
7

# 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")) 
3
it counts the number of elements in the list or in the string(words)
3

New to Communities?

Join the community