![]() |
VOOZH | about |
most_common() function is a method provided by the Counter class in Python's collections module. It returns a list of the n most common elements and their counts from a collection, sorted by frequency. Example:
[('apple', 3), ('banana', 2), ('orange', 1)]
most_common(n)
Parameters:
Returns: A list of tuples containing the n most common elements and their counts, sorted in descending order by frequency.
[('Ram', 2), ('and', 2), ('are', 2)]
Explanation: The function splits the text into words and counts how many times each word appears. The three most common words are returned along with their counts.
[('a', 5)]
Explanation: The function counts the occurrence of each character in the string and returns the most common character ('a') along with its frequency (5 times).
[('apple', 3)]
Explanation: The function counts how many times each item appears in the list and returns the most common item ('apple') with its frequency (3 times).
Related Articles: