how to count frequency of elements in a list in python code example
Example 1: list count frequency python
import collections
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
counter=collections.Counter(a)
print(counter)
print(counter.values())
print(counter.keys())
print(counter.most_common(3))
Example 2: freq count in python
freq = {}
for item in my_list:
if (item in freq):
freq[item] += 1
else:
freq[item] = 1
Example 3: python count the frequency of words in a list
from collections import Counter
list1=['apple','egg','apple','banana','egg','apple']
counts = Counter(list1)
print(counts)
Example 4: find frequency of numbers in list python
from collections import Counter
def frequency_table(n):
table = Counter(n)
print('Number\tFrequency')
for number in table.most_common() :
print('{0}\t{1}'.format(number[0], number[1]))