Finding most common elements
Another powerful usage of Counter is finding the most common elements in
a list. This can be done with the .most_common() method.
Practice using this now to find the most common species in a penguins list.
Latihan ini adalah bagian dari kursus
Data Types in Python
Petunjuk latihan
- Import the
Counterobject fromcollections. - Create a
Counterof thepenguinslist calledpenguins_species_counts; use a list comprehension to return theSpeciesof each penguin to the Counter. - Print the three most common species counts.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# Import the Counter object
____
# Create a Counter of the penguins list: penguins_species_counts
penguins_species_counts = ____
# Find the 3 most common species counts
print(____)