Sometimes, while working with Numbers in Python, we can have a problem in which we need to get the number of time one number follows other. This can have application in many domains such as day-day programming and other domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is brute force way in which we perform this task. In this, we traverse list and keep checking for previous and current number for result and keep incrementing the counter.
# Python3 code to demonstrate working of # Frequency of x follow y in Number # using loop def count_occ(num, next, prev): num = str(num) prev_chr = "" count = 0 for chr in num: if chr == next and prev_chr == prev: count += 1 prev_chr = chr return count # initialize Number test_num = 12457454 # printing original number print("The original number : " + str(test_num)) # initialize i, j i, j = '4', '5' # Frequency of x follow y in Number # using loop res = count_occ(test_num, j, i) # printing result print("The count of x preceding y : " + str(res)) |
The original number : 12457454 The count of x preceding y : 2
Method #2 : Using loop + isinstance() + regex
This is yet another way in which this task can be performed. In this, we use regex function to check the preceding term rather than brute approach.
# Python3 code to demonstrate working of # Frequency of x follow y in Number # using loop + isinstance() + regex import re def count_occ(num, next, prev): if not isinstance(num, str): num = str(num) return len(re.findall(prev + next, num)) # initialize Number test_num = 12457454 # printing original number print("The original number : " + str(test_num)) # initialize i, j i, j = '4', '5' # Frequency of x follow y in Number # using loop + isinstance() + regex res = count_occ(test_num, j, i) # printing result print("The count of x preceding y : " + str(res)) |
The original number : 12457454 The count of x preceding y : 2
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Check whether two lists follow same pattern or not
- Python | Check if all elements in list follow a condition
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Find frequency of each word in a string in Python
- Python | Count all prefixes in given string with greatest frequency
- Python | Frequency of each character in String
- Python | Element with largest frequency in list
- Python | Finding frequency in list of tuples
- Python | List frequency of elements
- Python | Sort list elements by frequency
- Python | Find sum of frequency of given elements in the list
- Python | Delete elements with frequency atmost K
- Python | Sort given list by frequency and remove duplicates
- Python | Find frequency of given character at every position in list of lists
- Python | Extract least frequency element
- Python | Frequency of substring in given string
- Python | Group list elements based on frequency
- Python | Frequency grouping of list elements
- Python | Construct string from character frequency tuple
- Python | Find frequency of largest element in list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

