Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

separate and count emojis in python list

I want to count the occurrences of Emojis in a list in python.

Assuming my list looks like this
li = ['😁', '🤣😁', '😁🤣😋']

Counter(li) would give me {'😁': 1, '🤣😁': 1, '😁🤣😋': 1}

But I would like to get the total amount of emojis aka {'😁': 3, '🤣': 2, '😋': 1}

My main issue is how to seperate large chunks of continous emoji into single list entries. I tried with replacing the beginning "\U" with " \U" so i could then simple split by " " but it does not seem to work.

Thanks for your help in advance :)

like image 762
Pewtas Avatar asked Aug 06 '26 13:08

Pewtas


1 Answers

You can flatten you list into a single string using join and then apply Counter to that:

Counter("".join(li))

results in

Counter({'😁': 3, '🤣': 2, '😋': 1})

or maybe a more memory efficient way is

counter = Counter()
for item in li:
    counter.update(item)
like image 179
Dan Avatar answered Aug 08 '26 07:08

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!