Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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. 
Comment

find frequency of numbers in list python

from collections import Counter

def frequency_table(n):
    table = Counter(n)
    print('Number	Frequency')
    for number in table.most_common() :
        print('{0}	{1}'.format(number[0], number[1]))
        
# src : Doing Math With Python
Comment

Count Frequency of elements in array using python

def countFreq(arr):
    visited = [False for _ in range(len(arr))]
    
    for i in range(len(arr)):
        count = 1
        if visited[i]:
            continue
        for j in range(i+1, len(arr)):
            if arr[i] == arr[j]:
                count += 1
                visited[j] = True
        visited[i] = True
        print(f'{arr[i]} : {count}')

# Time complexity: O(n * n)
# space complexity: O(n)
Comment

PREVIOUS NEXT
Code Example
Python :: user agent for python 
Python :: print current time hours and minutes in python 
Python :: python requests get title 
Python :: check if a list contains an item from another list python 
Python :: pandas to csv without header 
Python :: open chrome in pyhton 
Python :: count none in list python 
Python :: python function to print random number 
Python :: pandas concat and reset index 
Python :: python take a screenshot 
Python :: dataframe slice by list of values 
Python :: remove unicode characters from string python 
Python :: filter by row contains pandas 
Python :: django-admin command not found 
Python :: how to send whatsapp message with python 
Python :: check python version ubuntu 
Python :: pytesseract tesseract is not installed 
Python :: char to binary python 
Python :: utf8 python encodage line 
Python :: install wxpython 
Python :: how to add list item to text file python 
Python :: python copy dir 
Python :: pygame fullscreen 
Python :: parse youtube video id from youtube link python 
Python :: python create nested directory 
Python :: tensorflow turn off gpu 
Python :: converting string array to int array python 
Python :: pyplot define plotsize 
Python :: django reverse 
Python :: load saved model 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =