Reputation: 53
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 :)
Upvotes: 2
Views: 599
Reputation: 31339
One more way is to use the fact that counters implement addition:
>>> li = ['😁', '🤣😁', '😁🤣😋']
>>> from collections import Counter
>>> sum(map(Counter, li), Counter())
Counter({'😁': 3, '🤣': 2, '😋': 1})
Upvotes: 0
Reputation: 24232
You can count the emojis by iterating on the characters of each string:
from collections import Counter
li = ['😁', '🤣😁', '😁🤣😋']
count = Counter(emoji for string in li for emoji in string)
print(count)
# Counter({'😁': 3, '🤣': 2, '😋': 1})
@Dan gave a different answer just before me, which he sadly deleted since then, so I reproduce it for <10k users who can't see it:
Counter("".join(li))
I thought it might be less efficient because of the creation of the joined string, but I did some timings with small and larger lists up to 10 000 000 items, and it appears that his solution is consistently 30 to 40% faster.
Upvotes: 2
Reputation: 45752
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)
Upvotes: 6