Python | Consecutive characters frequency
Last Updated :
21 Aug, 2023
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 Frequency
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
test_str = "geekksforgggeeks"
print ( "The original string is : " + test_str)
res = [ len ( list (j)) for _, j in groupby(test_str)]
print ( "The Consecutive characters frequency : " + str (res))
|
Output
The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Count Consecutive Characters frequency using a loop
Here, we are using 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
test_str = "geekksforgggeeks"
print ( "The original string is : " + test_str)
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)
print ( "The Consecutive characters frequency : " + str (res))
|
Output
The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Python Consecutive identical elements 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
test_str = "geekksforgggeeks"
print ( "The original string is : " + test_str)
res = [ len (sub.group()) for sub in re.finditer(r '(.)\1*' , test_str)]
print ( "The Consecutive characters frequency : " + str (res))
|
Output
The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Using itertools.groupby() to Count Consecutive Characters frequency
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
test_str = "geekksforgggeeks"
print ( "The original string is : " + test_str)
res = [ len ( list (group)) for key, group in itertools.groupby(test_str)]
print ( "The Consecutive characters frequency : " + str (res))
|
Output
The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Count Consecutive Characters using 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
from collections import Counter
test_str = "geekksforgggeeks"
print ( "The original string is : " + test_str)
res = []
count = 1
c = Counter(test_str)
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)
print ( "The Consecutive characters frequency : " + str (res))
|
Output
The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]
Please Login to comment...