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.
Count consecutive characters using list comprehension + groupby()
This is one of the shorthand with the help of which this task can be performed. In this, we employ groupby() to group consecutive together to perform frequency calculations.
Python3
from itertools import groupby # initializing string test_str = "geekksforgggeeks" # printing original string print ( "The original string is : " + test_str) # Consecutive characters frequency # Using list comprehension + groupby() res = [ len ( list (j)) for _, j in groupby(test_str)] # printing result print ( "The Consecutive characters frequency : " + str (res)) |
The original string is : geekksforgggeeks The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Count consecutive characters using a loop
First, we initialize a string variable named test_str with a value of “geekksforgggeeks”. Then we print the original string using the print statement with a message. Next, we initialize an empty list called res, which we will use to store the frequency of consecutive characters. We also initialize a variable called count to 1. This variable will keep track of the current count of consecutive characters. We use a for loop to iterate through the input string. For each character in the input string, we compare it with the next character in the string using an if statement. If the current character is the same as the next character, we increment the count variable by 1. If the current character is different from the next character, we append the current value of count to the res list and reset the value of count to 1. After iterating through the entire string, we append the final value of the count to the res list. Finally, we print the result using the print statement with a message.
Python3
# initializing string test_str = "geekksforgggeeks" # printing original string print ( "The original string is : " + test_str) # Consecutive characters frequency using loop res = [] count = 1 for i in range ( len (test_str) - 1 ): if test_str[i] = = test_str[i + 1 ]: count + = 1 else : res.append(count) count = 1 res.append(count) # printing result print ( "The Consecutive characters frequency : " + str (res)) |
The original string is : geekksforgggeeks The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Count consecutive characters using regex
Another way to solve this problem is using regex. In this, we employ the regex character-finding technique and find the count using len().
Python3
import re # initializing string test_str = "geekksforgggeeks" # printing original string print ( "The original string is : " + test_str) # Consecutive characters frequency # Using regex res = [ len (sub.group()) for sub in re.finditer(r '(.)\1*' , test_str)] # printing result print ( "The Consecutive characters frequency : " + str (res)) |
The original string is : geekksforgggeeks The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Count consecutive characters using itertools.groupby()
Import the itertools module. Initialize the input string. Use the itertools.groupby() function to group the consecutive characters in the string. Convert the grouped characters into a list and get the length of the list for each group of consecutive characters. Store the length of each group in a list. Print the list of lengths as the result.
Python3
import itertools # initializing string test_str = "geekksforgggeeks" # printing original string print ( "The original string is : " + test_str) # Consecutive characters frequency # Using itertools.groupby() res = [ len ( list (group)) for key, group in itertools.groupby(test_str)] # printing result print ( "The Consecutive characters frequency : " + str (res)) |
The original string is : geekksforgggeeks The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Count consecutive characters using the Counter() function
Import the collections module to use the Counter() function. Initialize an empty list ‘res’ to store the frequency of consecutive characters. Use the Counter() function to count the occurrence of each character in the string. Use a loop to iterate through the string, and check if the current character is equal to the next character. If it is, increment the count of consecutive characters, else append the count to the ‘res’ list, and reset the count to 1. Append the last count to the ‘res’ list. Print the ‘res’ list.
Python3
# importing the collections module from collections import Counter # initializing string test_str = "geekksforgggeeks" # printing original string print ( "The original string is : " + test_str) # Consecutive characters frequency using Counter() and loop res = [] count = 1 # using Counter() to count occurrence of each character c = Counter(test_str) # iterating through the string for i in range ( len (test_str) - 1 ): # checking if the current character is equal to the next character if test_str[i] = = test_str[i + 1 ]: count + = 1 else : res.append(count) count = 1 # appending the last count to the 'res' list res.append(count) # printing result print ( "The Consecutive characters frequency : " + str (res)) |
The original string is : geekksforgggeeks The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Please Login to comment...