Sometimes, while working with Python Strings, we can have a problem in which we need to check for all the characters indices. The position where they occur. This kind of application can come in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using set() + regex + list comprehension + replace()
The combination of above functions can be used to perform this task. In this, set() is used to get elements whose frequency has to be computed. The task of assembling to dictionary accordingly is performed using regex function and list comprehension.
# Python3 code to demonstrate working of # Characters Index occurrences in String # Using regex + set() + list comprehension + replace() import re # initializing string test_str = "Gfg is best for geeks" # printing original string print("The original string is : " + test_str) # Characters Index occurrences in String # Using regex + set() + list comprehension + replace() temp = set(test_str.replace(' ', '')) res = {ele: [sub.start() for sub in re.finditer(ele, test_str)] for ele in temp} # printing result print("Characters frequency index dictionary : " + str(res)) |
The original string is : Gfg is best for geeks
Characters frequency index dictionary : {'g': [2, 16], 'k': [19], 't': [10], 'G': [0], 'b': [7], 'i': [4], 'r': [14], 'f': [1, 12], 's': [5, 9, 20], 'o': [13], 'e': [8, 17, 18]}
Method #2 : Using loop + enumerate()
This is yet another way in which this task can be performed. In this we create a dictionary and then iterate the string to map the characters with their respective characters.
# Python3 code to demonstrate working of # Characters Index occurrences in String # Using loop + enumerate() import re # initializing string test_str = "Gfg is best for geeks" # printing original string print("The original string is : " + test_str) # Characters Index occurrences in String # Using loop + enumerate() res = {ele : [] for ele in test_str} for idx, ele in enumerate(test_str): res[ele].append(idx) # printing result print("Characters frequency index dictionary : " + str(res)) |
The original string is : Gfg is best for geeks
Characters frequency index dictionary : {'g': [2, 16], 'k': [19], 't': [10], 'G': [0], 'b': [7], 'i': [4], 'r': [14], 'f': [1, 12], 's': [5, 9, 20], 'o': [13], 'e': [8, 17, 18]}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Get the starting index for all occurrences of given substring
- Python | Replace characters after K occurrences
- Python program to print k characters then skip k characters in a string
- Python | Count occurrences of a character in string
- Python | All occurrences of substring in string
- Python | Count String occurrences in mixed list
- Python - Replace all occurrences of a substring in a string
- Python - Like Index Common characters
- Dunn index and DB index - Cluster Validity indices | Set 1
- Count occurrences of a sub-string with one variable character
- Count occurrences of a word in string
- Python | Convert list of strings and characters to list of characters
- Python | Count occurrences of an element in a list
- Python | Count occurrences of an element in a Tuple
- Python | Deleting all occurrences of character
- Python | Remove all occurrences in nested list
- Python | Count occurrences of each word in given text file (Using dictionary)
- Python | Record Similar tuple occurrences
- Python - Extract elements with Range consecutive occurrences
- Python program to Count the Number of occurrences of a key-value pair in a text file
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.

