Created
December 5, 2019 01:34
-
-
Save FerdinaKusumah/0ed600b26105020429312802e0e7c5aa to your computer and use it in GitHub Desktop.
Most frequent in list
This file contains hidden or 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
from collections import Counter | |
"""Find most frequent in list""" | |
arr = [1, 2, 3, 1, 2, 4, 6, 7, 4, 3 ,2, 3, 4, 5] | |
cnt = Counter(arr) | |
print("""Showing all frequent in list""") | |
print(cnt) | |
# Counter({2: 3, 3: 3, 4: 3, 1: 2, 6: 1, 7: 1, 5: 1}) | |
print("""Show only frequent in 3""") | |
print(cnt.most_common(3)) | |
# [(2, 3), (3, 3), (4, 3)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment