0

I've this code that returns a dict of chars .. how can i find 10 chars with the highest count.

emoji_list = emoji_list[0:10000] #Sample_size of the emoji_list

def CountFrequency(my_list):
    
    freq = {}
    
    for char in my_list:
        if char in freq:
            freq[char] += 1
        else:
            freq[char] = 1
    print(freq)
            
dict = CountFrequency(emoji_list)  

THIS HOW THE RESULTS OUPUT LOOKS LIKE

{'‼': 16, '': 46, '': 214, '': 173, '': 115, '❤': 1096, '': 1101, '': 49, '': 44, '': 13, '': 1557, '': 325, '': 8, '': 2, '': 49, '': 100, '': 14, '': 243, '': 158, '': 121, '': 59, '': 11, '': 20, '': 4, '': 9, '': 52, '': 6, '': 12, '': 38, '': 143, '': 20, '': 60, '': 3, '': 20, '': 24, '': 11, '': 12, '™': 2, '☺': 37, '': 20, '': 56, '': 10, '': 109, '': 59, '': 10, '☹': 6, '': 11, '': 97, '': 47, '': 62, '': 1, '': 15, '': 8, '♀': 10, '': 23, '': 82, '✌': 30, '': 29, '': 19, '': 87, '': 23, '': 7, '': 37, '': 2, '': 26, '♂': 10, '': 36, '': 2, '': 88, '': 258, '': 36, '': 71, '': 11, '': 53, '': 55, '': 46, '': 20, '': 178, '': 16, '': 23, '': 18, '': 85, '': 27, '': 13, '': 1, '': 1, '': 2, '': 3, '': 2, '': 20, '✨': 24, '': 3, '': 55, '': 3, '': 15, '': 13, '': 24, '': 13, '': 12, '': 33, '': 14, '': 13, '': 33, '': 11, '': 9, '': 11, '♨': 2, '': 29, '': 33, '': 2, '': 7, '': 3, '': 2, '': 14, '': 21, '': 2, '': 7, '': 6, '': 6, '': 28, '': 3, '': 3, '': 21, '': 32, '': 27, '': 12, '': 12, '': 9, '': 13, '': 21, '': 5, '': 36, '': 8, '': 5, '': 11, '': 24, '': 12, '': 6, '': 11, '': 43, '': 38, '': 4, '♥': 85, '': 16
  • Essentially this is (or at least can be turned into) a question of sorting an existing dictionary by its values, which has been discussed here: https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value – Schnitte Mar 14 '22 at 09:39

3 Answers3

0

An easy way to do that is to use collections.Counter, and you can simply do

from collections import Counter
print(Counter(emoji_list).most_common(10))
Mia
  • 2,466
  • 22
  • 38
  • I don't want to use the collection count module... i want o learn how it's done traditionally for personal development. –  Mar 14 '22 at 09:42
0

You can do it as follows:

def count_frequency(my_list):
    
    freq = {}
    
    for char in my_list:
        if char in freq:
            freq[char] += 1
        else:
            freq[char] = 1
    ls = list(freq)
    # Sorts list in decreasing order 
    ls = sorted(ls, key=lambda x: -x[1])
    return [char for char, count in ls[:10]]
Alf
  • 36
  • 2
  • where can i pass the emoji_list, if i call `CountFrequency(emoji_list) ` at the end of the fucntion above it returns the default value –  Mar 14 '22 at 10:00
0

Try this:

def CountFrequency(my_list):
    freq = {}

    for char in my_list:
        if char in freq:
            freq[char] += 1
        else:
            freq[char] = 1
    return freq

dict = CountFrequency(emoji_list)
top_10 = {e: n for e, n in sorted(dict.items(), key=lambda  item: -item[1])[:10]}

print(top_10) # to get first 10 characters along with their count
print(list(top_10.keys())) # to get first 10 characters as list (without their respective count)