Created
March 12, 2020 08:10
-
-
Save ettorerizza/0275abdcc3b89c3a8f483e6d92ff62e8 to your computer and use it in GitHub Desktop.
Most common elements in a list (with ties)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def most_commons(List): | |
"""Return a new list with the most common elements | |
in a list | |
""" | |
from collections import Counter | |
count = Counter(List) | |
freq_list = count.values() | |
max_cnt = max(freq_list) | |
total = freq_list.count(max_cnt) | |
most_commons = count.most_common(total) | |
return [elem[0] for elem in most_commons] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment