Return Most_occurring 个字符

Return Most_occurring chars

我有这段代码 returns 一个字符字典..我怎样才能找到 10 个字符数最多的字符。

emoji_list = emoji_list[0:10000] #Sample_size 的 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

一个简单的方法是使用 collections.Counter,你可以简单地做

from collections import Counter
print(Counter(emoji_list).most_common(10))

您可以按照以下方式进行:

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]]

试试这个:

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)