Hey guys, Minh Vu here.
In this tutorial, I will show you how to count the occurrences or calculate the frequency of a character in a string in Python.
In short, we will use a dictionary to store the character as the key and the frequency as the value.
Table of Contents
To make sure everyone can understand, I will try to cover everything from basic to the solution.
- What is a Dictionary in Python?
- Count Character Occurrences in String in Python
- Sort the Occurrences Alphabetically
- Conclusion
What is a Dictionary in Python?
A dictionary is a data structure in Python that allows you store data in key-value pairs.
For example, you can store the information of a person using a dictionary like this:
person = { "name": "Minh Vu", "age": 21, "location": "Vietnam", }
Here, we created a dictionary called person
with 3 key-value pairs.
- Key:
"name"
, Value:"Minh Vu"
- Key:
"age"
, Value:21
- Key:
"location"
, Value:"Vietnam"
Similarly, we can use this to store the frequency of a character in a string.
For example, a string "wisecode"
has the following frequency of characters:
count = { "w": 1, "i": 1, "s": 1, "e": 2, "c": 1, "o": 1, "d": 1, }
Let's see how to do this in Python.
Count Character Occurrences in String in Python
To count the occurrences of a character in a string, we will loop through the characters in the string and count them all.
Here are the steps:
- Create a dictionary called
count
. - Loop through the characters in the string.
- If the character is not in the dictionary, add it to the dictionary with the value of
1
. - If the character is already in the dictionary, increase its value by
1
.
- If the character is not in the dictionary, add it to the dictionary with the value of
Here is the code:
def count_char_occurrences(text): count = {} for char in text: if char not in count: # we can optimize this by using count.get(char, 0) count[char] = 1 else: count[char] += 1 return count text = "wisecode" print(count_char_occurrences(text))
The output will be:
{ "w": 1, "i": 1, "s": 1, "e": 2, "c": 1, "o": 1, "d": 1 }
The optimized version of the code will be:
def count_char_occurrences(text): count = {} for char in text: count[char] = count.get(char, 0) + 1 return count text = "wisecode" print(count_char_occurrences(text))
Sort the Occurrences Alphabetically
To output the dictionary in an alphabetical order, we can use the sorted()
function.
def count_char_occurrences(text): count = {} for char in text: count[char] = count.get(char, 0) + 1 return count text = "wisecode" count = count_char_occurrences(text) count_alphabetically_sorted = sorted(count.items(), key=lambda x: x[0]) count_alphabetically_sorted = dict(count_alphabetically_sorted) print(count_sorted)
To help you understand better, here is the output of count.items()
:
dict_items([('w', 1), ('i', 1), ('s', 1), ('e', 2), ('c', 1), ('o', 1), ('d', 1)])
The sorted()
function will sort the items in the dictionary by the key.
In this case, we use a lambda function to sort the items by the key, which is x[0]
, as one item in the dictionary is a tuple with 2 values: x[0]
is the key and x[1]
is the value.
So the output will be:
{ "c": 1, "d": 1, "e": 2, "i": 1, "o": 1, "s": 1, "w": 1 }
As you can see, the characters are sorted alphabetically.
Conclusion
In general, to count the occurrences of a character in a string, we can use a dictionary to store the character as the key and the frequency as the value.
I will add more use cases as I face them so you can have a wider view of this problem.