我有一个文件如下,我想计算一下人们提到其他人的次数:
peter @amy
tom @amy
tom @amy
peter @tom
edwin @amy
amy @peter
tom @john @peter
amy @edwin
tom @peter
peter @john
peter @john
john @tom?
edwin @john
edwin @amy
amy @tom
我试着用:
for line in fhand:
if "@" in line:
indexStart = line.find("@")
但我不知道接下来会发生什么。预期输出为:
tom 5
amy 3
edwin 3
peter 5
john 1
有什么办法吗?你知道吗
选项1
re.findall
与collections.Counter
regex
说明:如果你真的需要他们提到人的次数,而不仅仅是他们提到人的行数:
选项2
使用
collections.defaultdict
:选项3 与
pandas
一起生活在边缘:相关问题 更多 >
编程相关推荐