我想使用collections.Counter类来计数字符串中的表情符号。但是,当我介绍彩色表情符号时,它通常工作得很好,表情符号的颜色成分与表情符号是分开的,如下所示:
>>> import collections
>>> emoji_string = ""
>>> emoji_counter = collections.Counter(emoji_string)
>>> emoji_counter.most_common()
[('', 5), ('', 1), ('', 1), ('', 1), ('', 1), ('', 1)]如何使most_common()函数返回如下内容:
[('', 1), ('', 1), ('', 1), ('', 1), ('', 1)]我使用Python3.6
发布于 2017-05-08 16:35:09
您将不得不将字符串拆分为不同的集群。您的每个表情符号实际上是两个代码点;该表情符号和一个符号修饰符FITZPATRICK类型为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'您可以使用一个正则表达式来保留前面的表情符号:
import re
char_with_modifier = re.compile(r'(.[\U0001f3fb-\U0001f3ff]?)')
split_emoji = char_with_modifier.findall(emoji_string)然后数数结果。
演示:
>>> 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})发布于 2020-11-24 18:42:02
import regex
from collections import Counter
emoji_string = ""
data = regex.findall(r'\X',emoji_string)
print(Counter(data))预期产出
Counter({'': 1, '': 1, '': 1, '': 1, '': 1})https://stackoverflow.com/questions/43852668
复制相似问题