How to Count Occurrence Frequency of a Particular Number in Python


This example will explain how to count the occurrence frequency of a particular number on the list in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

a = []
b = input('Input a number of quit by typing "q": ')
if b == "q":
    print("Quit the system!")
else:
    while b != "q":
        a.append(int(b))
        b = input('Input a number of quit by typing "q": ')
    k = 0
    n = int(input("Enter the number to be counted: "))
    for j in a:
        if j == n:
            k = k + 1
    print("The occurrence frequency of {} is : {}".format(n, k))

Output:

Input a number of quit by typing "q": 4
Input a number of quit by typing "q": 6
Input a number of quit by typing "q": 4
Input a number of quit by typing "q": 8
Input a number of quit by typing "q": 4
Input a number of quit by typing "q": 2
Input a number of quit by typing "q": q
Enter the number to be counted: 4
The occurrence frequency of 4 is: 3

Input a number of quit by typing "q": 18
Input a number of quit by typing "q": 16
Input a number of quit by typing "q": 89
Input a number of quit by typing "q": 16
Input a number of quit by typing "q": 23
Input a number of quit by typing "q": 16
Input a number of quit by typing "q": q
Enter the number to be counted: 16
The occurrence frequency of 16 is: 3
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement