使用collections.Counter计算不同颜色的表情符号

Ton*_*čić 4 python unicode counter emoji

我想使用collections.Counter类来计算字符串中的表情符号.它通常工作正常,但是,当我引入彩色表情符号时,表情符号的颜色成分与表情符号分开,如下所示:

>>> import collections
>>> emoji_string = ""
>>> emoji_counter = collections.Counter(emoji_string)
>>> emoji_counter.most_common()
[('', 5), ('', 1), ('', 1), ('', 1), ('', 1), ('', 1)]
Run Code Online (Sandbox Code Playgroud)

我怎样才能使most_common()函数返回这样的内容:

[('', 1), ('', 1), ('', 1), ('', 1), ('', 1)]
Run Code Online (Sandbox Code Playgroud)

我正在使用Python 3.6

Mar*_*ers 7

您必须将字符串拆分为单独的集群.你的每个表情符号都是两个代码点 ; 表情符号和EMOJI MODIFIER FITZPATRICK TYPE X代码点:

>>> print(emoji_string[0])

>>> print(emoji_string[1])

>>> print(emoji_string[:2])

>>> print(ascii(emoji_string[:2]))
'\U0001f44c\U0001f3fb'
>>> import unicodedata
>>> unicodedata.name(emoji_string[1])
'EMOJI MODIFIER FITZPATRICK TYPE-1-2'
Run Code Online (Sandbox Code Playgroud)

您可以使用正则表达式来保留前面的表情符号:

import re

char_with_modifier = re.compile(r'(.[\U0001f3fb-\U0001f3ff]?)')
split_emoji = char_with_modifier.findall(emoji_string)
Run Code Online (Sandbox Code Playgroud)

并计算结果.

演示:

>>> import re
>>> from collections import Counter
>>> emoji_string = ""
>>> char_with_modifier = re.compile(r'(.[\U0001f3fb-\U0001f3ff]?)')
>>> Counter(char_with_modifier.findall(emoji_string))
Counter({'': 1, '': 1, '': 1, '': 1, '': 1})
Run Code Online (Sandbox Code Playgroud)