![]() |
The 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: Here, we are using the most_common() function to arrange elements according to the frequency in decreasing order.
Output [('apple', 3), ('banana', 2), ('orange', 1)] Syntax of Python most_common() Function
Python most_common() Function ExamplesBelow are some of the commonly used examples of Python most_common() Function of collections module in Python: Example 1: Analyzing Word FrequencyIn this example, we use most_common() to find the three most common words in a piece of text.
Output [('Ram', 2), ('and', 2), ('are', 2)] Example 2: Counting Character OccurrencesHere, we utilize most_common() to identify the most common character and its count in a string.
Output [('a', 5)] Example 3: Identifying Popular ItemsThis example demonstrates using most_common() to determine the most popular item in a list along with its count.
Output [('apple', 3)] |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |