繁体   English   中英

计算列表中两个标记之间的子字符串数

[英]Count the number of substrings between two markers in a list

我想计算以下列表中提及该名称的次数,并将提及的次数和频率放入字典中。

所以,例如,这是对话列表

dialogue = ["This is great! RT @user14: Can you believe this?",
            "That's right RT @user22: The dodgers are destined to win the west!",
            "This is about things @user14, how could you",
            "RT @user11: The season is looking great!"]

我希望我的 output 是{user14:2, user22:1, user11:1}

我尝试开始编写以下内容以生成名单,然后将名单和 output 计数到字典中。 但不知道如何做到这一点

user_name = [x.split('@')[1].split(':')[:-1] for x in tweets]

正则表达式可能是解释用户名后未知字符的最佳方法:

from collections import defaultdict
import re

result = defaultdict(int)

for item in dialogue:

    user = re.search('(?<=@)[\w\s]+', item).group(0)
    result[user] += 1

print(result)

给出:

{'user14': 2, 'user22': 1, 'user11': 1}

在单程中使用collections.Counter object 和re.findall function:

from collections import Counter
import re

...
uname_counts = Counter(re.findall(r'@(\w+)', ''.join(dialogue)))
print(dict(uname_counts))   # {'user14': 2, 'user22': 1, 'user11': 1}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM