Python – Successive Characters Frequency

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 above methods constitute the brute force method to perform this task. In this, we perform the task of counting using count(), and character is searched using findall().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original string is : geeksforgeeks is best for geeks. A geek should take interest.
The Characters Frequency is : {'s': 3, ' ': 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.