- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add the occurrence of each number as sublists in Python
We have a list whose elements are numeric. Many elements are present multiple times. We want to create sub list so the frequency of each element along with the elements itself.
With for and append
In this approach we will compare each element in the list with every other elements after it. If there is a match then count will be incremented and both the element and the count will be made into a subsist. List will be made which should contain subsists showing every element and its frequency.
Example
def occurrences(list_in): for i in range(0, len(listA)): a = 0 row = [] if i not in listB: for j in range(0, len(listA)): # matching items from both positions if listA[i] == listA[j]: a = a + 1 row.append(listA[i]) row.append(a) listB.append(row) # Eliminate repetitive list items for j in listB: if j not in list_uniq: list_uniq.append(j) return list_uniq # Caller code listA = [13,65,78,13,12,13,65] listB = [] list_uniq = [] print("Number of occurrences of each element in the list:\n") print(occurrences(listA))
Output
Running the above code gives us the following result −
Number of occurrences of each element in the list: [[13, 3], [65, 2], [78, 1], [12, 1]]
With Counter
We use counter method from the collections module. It will give the count of every element in the list. Then we declare a new empty list and add the key value pair for each item in form of element and its count into the new list.
Example
from collections import Counter def occurrences(list_in): c = Counter(listA) new_list = [] for k, v in c.items(): new_list.append([k, v]) return new_list listA = [13,65,78,13,12,13,65] print("Number of occurrences of each element in the list:\n") print(occurrences(listA))
Output
Running the above code gives us the following result −
Number of occurrences of each element in the list: [[13, 3], [65, 2], [78, 1], [12, 1]]
- Related Articles
- First occurrence of True number in Python
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Program to find number of sublists whose sum is given target in python
- Program to count number of sublists with exactly k unique elements in Python
- Program to find number of sublists that contains exactly k different words in Python
- Python program to find occurrence to each character in given string
- Python program to remove each y occurrence before x in List
- Program to find number of sublists with sum k in a binary list in Python
- Adding value to sublists in Python
- Java program to check occurrence of each character in String
- Java program to check occurrence of each vowel in String
- Count unique sublists within list in Python
- Program to find sum of the sum of all contiguous sublists in Python
- Program to find number of sublists we can partition so given list is sorted finally in python
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
