Last updated: Apr 10, 2024
Reading timeยท3 min
To find the most frequent character in a string:
collections.Counter()
class to create a counter object.most_common()
method to get the most frequent character in the
string.from collections import Counter string = 'aaabbc' counter = Counter(string) # โ Find the most frequent character in a string most_frequent = counter.most_common(1)[0] print(most_frequent) # ๐๏ธ ('a', 3) print(most_frequent[0]) # ๐๏ธ 'a' # โ Find the N most frequent characters in a string most_frequent_2 = counter.most_common(2) print(most_frequent_2) # ๐๏ธ [('a', 3), ('b', 2)]
We used the collections.Counter
class to count the occurrences of each
character.
The
Counter
class from the collections
module is a subclass of the dict
class.
The class is basically a mapping of key-count pairs.
from collections import Counter string = 'aaabbc' counter = Counter(string) print(counter) # ๐๏ธ Counter({'a': 3, 'b': 2, 'c': 1})
Counter
objects implement a
most_common()
method that returns a list of the N most common elements and their counts from
the most common to the least.
from collections import Counter string = 'aaabbc' counter = Counter(string) print(counter) # ๐๏ธ Counter({'a': 3, 'b': 2, 'c': 1}) most_frequent = counter.most_common(1)[0] print(most_frequent) # ๐๏ธ ('a', 3) print(most_frequent[0]) # ๐๏ธ 'a'
Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(my_list) - 1
.
If no argument is provided to the most_common
method, it lists all character
counts.
You can also use this approach to get the most common N characters in a string.
from collections import Counter string = 'aaabbc' counter = Counter(string) print(counter) # ๐๏ธ Counter({'a': 3, 'b': 2, 'c': 1}) most_frequent_2 = counter.most_common(2) print(most_frequent_2) # ๐๏ธ [('a', 3), ('b', 2)]
We passed 2 as an argument to the most_common()
method, so it returned the
most common 2 characters in the string.
Alternatively, you can use the max()
function.
This is a four-step process:
max()
function.key
argument to the functions.str.count()
method to count the occurrences of each character.max()
function will return the most common character.string = 'aaabbc' most_common = max(set(string), key=string.count) print(most_common) # ๐๏ธ 'a'
We used the max()
function to get the most common character in a string.
The max() function returns the largest item in an iterable or the largest of two or more arguments.
my_list = [15, 45, 30] result = max(my_list) print(result) # ๐๏ธ 45
The max
function also takes an optional key
argument.
key
argument specifies a one-argument ordering function.We used the set() class
to convert the string to a set
object to remove any duplicates.
You can imagine that:
str.count()
method gets called with each element in the set
.max()
function returns the most common character.string = 'aaabbc' most_common = max(set(string), key=string.count) print(most_common) # ๐๏ธ 'a'
The str.count method returns the number of occurrences of a substring in a string.
print('bobby'.count('b')) # ๐๏ธ 3 print('bobby'.count('x')) # ๐๏ธ 0
Alternatively, you can use the statistics.mode()
method.
The mode()
method returns the single most common value in the provided
iterable.
from statistics import mode string = 'aaabbc' most_common = mode(string) print(most_common) # ๐๏ธ 'a'
The statistics.mode() method takes an iterable and returns the most common value in the iterable.
If there are multiple values with the same frequency, the method returns the first encountered value.
If the provided iterable is empty, the method raises a StatisticsError
exception.
Prior to Python v3.8, the statistics.mode()
method raises a StatisticsError
exception if there is no most common element in the iterable, e.g. if the top
two have the same number of occurrences.
You can learn more about the related topics by checking out the following tutorials: