Answers for "how to get frequency of each elements in a python lists"

9

how to get frequency of each elements in a python list

# easiest way to count the frequency of all elements in a list
lst = ['Sam', 'Sam', 'Tim', 'Tim', 'Tim', 'r', 'l']

freq = {} # stores the frequency of elements
counting = [freq.update({x: lst.count(x)}) for x in lst]

# output of freq 
{'Sam': 2, 'Tim': 3, 'r': 1, 'l': 1}

#credit: buggyprogrammer.com
#Note: if you print "counting" it will return a list full of None so ignore it.
Posted by: Guest on May-24-2021
2

find frequency of numbers in list python

from collections import Counter

def frequency_table(n):
    table = Counter(n)
    print('NumbertFrequency')
    for number in table.most_common() :
        print('{0}t{1}'.format(number[0], number[1]))
        
# src : Doing Math With Python
Posted by: Guest on December-29-2020

Code answers related to "how to get frequency of each elements in a python lists"

Python Answers by Framework

Browse Popular Code Answers by Language