We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A Counter takes a collection of objects, in this case the list of strings created by input().split(), and creates a dictionary in which keys are the objects and the values are the quantity of each object. The most_common(n) method allows us to create a list of the n most common objects. A.most_common(1)[0][1] gives use the quantity of the most common element. Here's an example from the Python3 docs:
Equalize the Array
You are viewing a single comment's thread. Return to all comments →
A Counter takes a collection of objects, in this case the list of strings created by
input().split()
, and creates a dictionary in which keys are the objects and the values are the quantity of each object. Themost_common(n)
method allows us to create a list of then
most common objects.A.most_common(1)[0][1]
gives use the quantity of the most common element. Here's an example from the Python3 docs:Counter
has the nice property that it runs inO(n)
time so it's pretty much the fastest way to count occurances of an object from any iterable.