
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Fractional Frequency of elements in List
When it is required to find the fractional frequency of elements in a list, a dictionary comprehension, a simple iteration and the ‘Counter’ method is used.
Example
Below is a demonstration of the same −
from collections import Counter my_list = [14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65] print("The list is :") print(my_list) my_num = {index : 0 for index in set(my_list)} my_denominator = Counter(my_list) my_result = [] for element in my_list: my_num[element] += 1 my_result.append(str(my_num[element]) + '/' + str(my_denominator[element])) print("The result is :") print(my_result)
Output
The list is : [14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65] The result is : ['1/2', '1/1', '1/1', '1/2', '1/1', '1/1', '1/1', '1/1', '2/2', '2/2', '1/1', '1/1']
Explanation
The required packages are imported into the environment.
A list of integers is defined and is displayed on the console.
A dictionary comprehension is used to get unique elements from the list.
This is assigned to a variable.
A counter is created from the list.
An empty list is defined.
The list is iterated over, and the ‘/’ operator is used to add specific elements to the empty list using ‘append’ method.
This is the output that is displayed on the console.
- Related Articles
- List frequency of elements in Python
- Python – Restrict Elements Frequency in List
- Find sum of frequency of given elements in the list in Python
- Finding frequency in list of tuples in Python
- Matplotlib – Make a Frequency histogram from a list with tuple elements in Python
- Count Frequency of Highest Frequent Elements in Python
- Python – Count frequency of sublist in given list
- Count minimum frequency elements in a linked list in C++
- Element with largest frequency in list in Python
- Delete elements with frequency atmost K in Python
- Delete List Elements in Python
- Python – Adjacent elements in List
- Program to sort array by increasing frequency of elements in Python
- Assign range of elements to List in Python
- Python – List Elements Grouping in Matrix

Advertisements