Answers for "counting the frequency of elements in a list using a dictionary in python"

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)
# 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)]
Posted by: Guest on July-15-2020
3

freq count in python

freq = {} 
    for item in my_list: 
        if (item in freq): 
            freq[item] += 1
        else: 
            freq[item] = 1
Posted by: Guest on July-21-2020

Code answers related to "counting the frequency of elements in a list using a dictionary in python"

Python Answers by Framework

Browse Popular Code Answers by Language