Open In App

Python – Successive Characters Frequency

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python strings, we can have a problem in which we need to find the frequency of next character of a particular word in string. This is quite unique problem and has the potential for application in day-day programming and web development. Let’s discuss certain ways in which this task can be performed.

Input : test_str = 'geeks are for geeksforgeeks', que_word = "geek" 
Output : {'s': 3} 
Input : test_str = 'geek', que_word = "geek" 
Output : {}

Method #1 : Using loop + count() + re.findall() 

The combination of the above methods constitutes the brute force method to perform this task. In this, we perform the task of counting using count(), and the character is searched using findall() function. 

Python3




# Python3 code to demonstrate working of
# Successive Characters Frequency
# Using count() + loop + re.findall()
import re
     
# initializing string
test_str = 'geeksforgeeks is best for geeks. A geek should take interest.'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing word
que_word = "geek"
 
# Successive Characters Frequency
# Using count() + loop + re.findall()
temp = []
for sub in re.findall(que_word + '.', test_str):
    temp.append(sub[-1])
 
res = {que_word : temp.count(que_word) for que_word in temp}
 
# printing result
print("The Characters Frequency is : " + str(res))


Output : 

The original string is : geeksforgeeks is best for geeks. A geek should take interest.
The Characters Frequency is : {'s': 3, ' ': 1}

Method #2 : Using Counter() + list comprehension + re.findall() 

The combination of the above functions is used to perform the following task. In this, we use Counter() instead of count() to solve this problem. Works with newer versions of Python. 

Python3




# Python3 code to demonstrate working of
# Successive Characters Frequency
# Using Counter() + list comprehension + re.findall()
from collections import Counter
import re
 
# initializing string
test_str = 'geeksforgeeks is best for geeks. A geek should take interest.'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing word
que_word = "geek"
 
# Successive Characters Frequency
# Using Counter() + list comprehension + re.findall()
res = dict(Counter(re.findall(f'{que_word}(.)', test_str,
                              flags=re.IGNORECASE)))
 
# printing result
print("The Characters Frequency is : " + str(res))


Output : 

The original string is : geeksforgeeks is best for geeks. A geek should take interest.
The Characters Frequency is : {'s': 3, ' ': 1}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using operator.countOf() 

Python3




# Python3 code to demonstrate working of
# Successive Characters Frequency
# Using operator.countOf() + loop + re.findall()
import re
import operator as op
 
# initializing string
test_str = 'geeksforgeeks is best for geeks. A geek should take interest.'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing word
que_word = "geek"
 
# Successive Characters Frequency
# Using operator.countOf() + loop + re.findall()
temp = []
for sub in re.findall(que_word + '.', test_str):
    temp.append(sub[-1])
 
res = {que_word: op.countOf(temp, que_word) for que_word in temp}
 
# printing result
print("The Characters Frequency is : " + str(res))


Output

The original string is : geeksforgeeks is best for geeks. A geek should take interest.
The Characters Frequency is : {'s': 3, ' ': 1}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using a loop and dictionary

  1. Initialize the input string and the queried word.
  2. Initialize an empty dictionary to store the frequency of successive characters.
  3. Loop through the input string, checking if each substring of length len(que_word) starting at each index of the string is equal to the queried word.
  4. If a substring is equal to the queried word, extract the character immediately following the substring.
  5. If the character is already a key in the dictionary, increment its value by 1. Otherwise, add the character as a key with a value of 1.
  6. Once the loop completes, print the dictionary with the character frequencies.

Example:

Python3




# initializing string
test_str = 'geeksforgeeks is best for geeks. A geek should take interest.'
 
# initializing word
que_word = 'geek'
 
# initializing dictionary to store character frequencies
freq_dict = {}
 
# loop through the string and count successive character frequencies
for i in range(len(test_str)-1):
    if test_str[i:i+len(que_word)] == que_word:
        char = test_str[i+len(que_word)]
        if char in freq_dict:
            freq_dict[char] += 1
        else:
            freq_dict[char] = 1
 
# print the result
print('The Characters Frequency is:', freq_dict)


Output

The Characters Frequency is: {'s': 3, ' ': 1}

Time complexity: O(n), where n is the length of the input string. 
Auxiliary space: O(n), as we are storing a dictionary with potentially n/2 keys (if every character in the string follows the queried word) and their corresponding frequencies.

Method #5: Using regex search() and defaultdict()

Step-by-step approach:

  1. Initialize the input string test_str to the value ‘geeksforgeeks is best for geeks. A geek should take interest.’.
  2. Initialize the query word que_word to the value ‘geek’.
  3. Initialize an empty dictionary freq_dict using defaultdict(int), which allows us to set the initial value of each key to 0.
  4. Loop through all the matches of the regular expression pattern que_word + ‘(.)’ in the input string test_str.
  5. For each match, retrieve the character following the query word, which is captured in the first group of the regular expression pattern. Increment the count of that character in the freq_dict dictionary.
  6. After processing all the matches, print the frequency dictionary as a regular dictionary using the dict() constructor.
  7. The output of the program is the characters frequency dictionary, where the keys are the characters following the query word in the input string and the values are their respective frequencies.

Example:

Python3




import re
from collections import defaultdict
 
test_str = 'geeksforgeeks is best for geeks. A geek should take interest.'
que_word = 'geek'
 
freq_dict = defaultdict(int)
 
for match in re.finditer(que_word + '(.)', test_str):
    freq_dict[match.group(1)] += 1
 
print('The Characters Frequency is:', dict(freq_dict))


Output

The Characters Frequency is: {'s': 3, ' ': 1}

Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(k), where k is the number of distinct characters following the query word.

Method  #6: Using itertools.groupby() and Counter()

Step-by-step approach:

  • Import the itertools and Counter modules.
  • Use the re.findall() function to find all occurrences of the query word followed by a character in the input string test_str.
  • Use the itertools.groupby() function to group the characters following the query word.
  • Use the Counter() function to count the frequency of each group.
  • Print the result.

Python3




import re
import itertools
from collections import Counter
 
test_str = 'geeksforgeeks is best for geeks. A geek should take interest.'
que_word = 'geek'
 
matches = re.findall(que_word + '(.)', test_str)
groups = itertools.groupby(matches)
 
freq_dict = Counter([char for _, char_group in groups for char in char_group])
 
print('The Characters Frequency is:', freq_dict)


Output

The Characters Frequency is: Counter({'s': 3, ' ': 1})

Time complexity: O(n), where n is the length of the input string test_str.
Auxiliary space: O(k), where k is the number of unique characters following the query word.



Similar Reads

Python | Generate successive element difference list
While working with python, we usually come by many problems that we need to solve in day-day and in development. Specially in development, small tasks of python are desired to be performed in just one line. We discuss some ways to compute a list consisting of elements that are successive difference in the list. Method #1 : Using list comprehension
5 min read
Python | Find the equivalent discount in successive discounts in percentages
You are given n successive discounts in percentages. Your task is to find the equivalent discount in percentage. Input will contain a list in which each element of the list will be discount in percentage that will be negative in sign. Examples: Input : a = [-10, -35, -60, -75] Output : -94.14 Input : a = [-5, -20, -10.-23] Output : -49.08 SUCCESSIV
2 min read
Python - Successive element pairing
Sometimes, while working with Python list, we can have a problem in which we need to construct tuples, with the succeeding element, whenever that element matches a particular condition. This can have potential application in day-day programming. Let’s discuss a way in which this task can be performed. Method : Using zip() + list comprehension This
4 min read
Python - Extract Strings with Successive Alphabets in Alphabetical Order
Given a string list, extract list which has any succession of characters as they occur in alphabetical order. Input : test_list = ['gfg', 'ij', 'best', 'for', 'geeks'] Output : ['ij', 'gfg', 'best'] Explanation : i-j, f-g, s-t are consecutive pairs. Input : test_list = ['gf1g', 'in', 'besht', 'for', 'geeks'] Output : [] Explanation : No consecutive
5 min read
Python program to find Successive row difference in Matrix
Given a Matrix, the task is to write a Python program to perform differences from the previous row on the basis of the elements present. Input : test_list = [[5, 6, 3, 1], [7, 5, 3, 1], [3, 2], [7, 3, 3, 2], [2, 3], [9, 8, 1]] Output : [[], [7], [2], [7], [], [8, 9, 1]] Explanation : Comparing 1st and 2nd row, only 7 exists in 2nd row and not in 1s
7 min read
Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
Given a string S, the task is to find the prefix of string S with the maximum possible length such that frequency of each character in the prefix is at most the number of characters in S with minimum frequency. Examples: Input: S = 'aabcdaab' Output: aabcd Explanation: Frequency of characters in the given string - {a: 4, b: 2, c: 1, d: 1} Minimum f
8 min read
Python | Consecutive characters frequency
Sometimes, while working with Python, we can have a problem in which we need to compute the frequency of consecutive characters till the character changes. This can have applications in many domains. Let us discuss certain ways in which this task can be performed in Python. Python Program to Count Consecutive Characters FrequencyUsing list comprehe
5 min read
Python - Odd Frequency Characters
Sometimes, while working with Python strings, we can have a problem in which we need to extract all the string characters which have odd number of occurrences. This problem can have applications in domains such as data domain and day-day programming. Let's discuss certain ways in which this task can be performed. Illustrations: Input : test_str = '
6 min read
Python - Specific Characters Frequency in String List
Given a String list, extract frequency of specific characters in the whole strings list. Input : test_list = ["geeksforgeeks is best for geeks"], chr_list = ['e', 'b', 'g', 'f'] Output : {'g': 3, 'e': 7, 'b': 1, 'f' : 2} Explanation : Frequency of certain characters extracted. Input : test_list = ["geeksforgeeks"], chr_list = ['e', 'g'] Output : {'
5 min read
Check if a substring exists having only 2 distinct characters with frequency of one as twice the others
Given a string str[] of N lower case English alphabets, the task is to check if there exists a substring of the given string such that the substring is composed of only two characters and the frequency of 1st character = 2 * frequency of 2nd character. Example: Input: str[] = "aaaabbc"Output: YesExplanation: The substring str[0... 5] = "aaaabb" of
6 min read
Practice Tags :
three90RightbarBannerImg