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 character changes. This can have application in many domains. Lets discuss certain ways in which this task can be performed.

Method #1 : 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 consecutives together to perform frequency calculations.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Consecutive characters frequency
# Using list comprehension + groupby()
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)) 

chevron_right


Output :

The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]

 

Method #2 : Using regex
Another way to solve this problem is using regex. In this we employ regex character finding technique and find count using len().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Consecutive characters frequency
# Using regex
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)) 

chevron_right


Output :

The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.