'Return Most_occurring chars
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
Solution 1:[1]
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]]
Solution 2:[2]
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))
Solution 3:[3]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alf |
| Solution 2 | Mia |
| Solution 3 | Prabhakar Kalaiselvan |
